Python

Python AssertDictEqual

Python comes up with pretty much similar name functions. The assertDicEqual method is similar in working with the assertEqual method that has been used to compare the equality of two values. The only difference is that the assertDictEqual method is used to make an equality comparison between two dictionaries of python. Although the assertEqual method can also be applied to dictionaries, sometimes it fails. To avoid that, we use the assertDictEqual method. Therefore, we will see the assertDictEqual method today in this guide. Let’s start.

Example 01:

Let’s take the first example to simply compare two dictionaries within the Spyder3 code without using the assertDictEqual function to understand the equality concept simply. We have started our code with the initialization of two dictionary data structures. The dictionaries contain 3 key-pair values, i.e., similar in values but different in location. The print statement utilizes the “==” operator to display the comparison result of two dictionaries. As both the dictionaries are the same, they must return “True” as a result.

Dic1 = {'One': 46, 'Three': [2,14], 'Two': 14}

Dic1 = {'Two': 14, 'One': 46, 'Three': [2,14]}

print(Dic1 == Dic2)

After running this simple code, we have got “true” as output, i.e., dictionaries are equal.

Example 02:

Let’s take a look at the use of the assertEqual function on the dictionaries first. To use this function, we have first imported the “unittest” module of python. The built-in “TestStringMethods” class is used while implementing the TestCase module via the unittest library. The user-defined “test” function is defined. It contains the initialization of two dictionaries, “Dic1” and “Dic2”, with the same key-pair values. The assertEqual function is called here, taking Dic1 and Dic2 dictionaries as arguments to check the equality of dictionaries. It will return True if dictionaries are equal; otherwise, throw a test case FAILED exception.

import unittest
class TestStringMethods(unittest.TestCase):
    def test2(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 2}
self.assertEqual(Dic1, Dic2)
if __name__ == '__main__':
unittest.main()

After running this python code piece, we got the success message, i.e., “OK.” It means both dictionaries are equal and the same in values, and the test case is passed.

Example 03:

Here is another example of using the assertDicEqual function of python in our code. You will see that it will be very similar to the assertEqual method. We have declared the same dictionaries Dic1 and Dic2 in the code as in the above illustration within the test function. Also, we have initialized another string variable, “m,” to be used in the code. The only change is that the assertDictEqual function used here takes 3 arguments instead of 2. So, the assertDictEqual method uses 3 variables, i.e., v1, v2, and m. The variables v1 and v2 will be compared, while the variable “m” will show the success and failure of this comparison.

import unittest
class TestStringMethods(unittest.TestCase):
    def test2(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 2}
        m = "Dictionaries are not equal ..."
self.assertDictEqual(Dic1, Dic2, m)
if __name__ == '__main__':
unittest.main()

As the dictionaries initialized in the code are the same and equal; thus the output of this code shows the success message for the test case, i.e., “OK.”

Let’s change the code a little bit. This time, we have altered the dictionary values. Within the Dic2 dictionary, we have updated the value of key “Two” i.e. replaced it with 45. Also, we have updated the variable “m” i.e. as the dictionaries are not equal now.

import unittest
class TestStringMethods(unittest.TestCase):
    def test2(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 45}
        m = "Dictionaries are not equal ..."
self.assertDictEqual(Dic1, Dic2, m)
if __name__ == '__main__':
unittest.main()

After running this test code, we have got the FAIL output. This is called negative output for the test case, and hence our unit test was unsuccessful, i.e., dictionaries are not equal.

Example 04:

Let’s take a look at our last but not the least example of using the assertDictEqual function in the python test case code. The same header files, i.e., unittest, have been used. The same class and the main() function is utilized in the code. This python code uses two user-defined functions to check two test cases, i.e., test1 and test2. The output will be positive only when both the test cases are True. You can see that both the functions contain the same name dictionaries, i.e., Dic1 and Dic2. The “test1” function dictionaries are the same, while the test2 function dictionaries are not the same, i.e., change at key “Two” in Dic2. The “m” variable in both test functions contains different values.

import unittest
class TestStringMethods(unittest.TestCase):
    def test1(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 2}
        m = "Dictionaries are equal ..."
self.assertDictEqual(Dic1, Dic2, m)
    def test2(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 45}
        m = "Dictionaries are not equal ..."
self.assertDictEqual(Dic1, Dic2, m)
if __name__ == '__main__':
unittest.main()

Upon the execution of this code, we have got a negative result.

Let’s update the code by making both the test function dictionaries a little different from each other i.e. Dic1 and Dic2 are different.

import unittest
class TestStringMethods(unittest.TestCase):
    def test1(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 456}
        m = "Dictionaries are equal ..."
self.assertDictEqual(Dic1, Dic2, m)
    def test2(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 45}
        m = "Dictionaries are not equal ..."
self.assertDictEqual(Dic1, Dic2, m)
if __name__ == '__main__':
unittest.main()

You will see that the output shows two failures.

Let’s make the dictionaries similar and equal for both test functions now as below.

import unittest
class TestStringMethods(unittest.TestCase):
    def test1(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 2}
        m = "Dictionaries are equal ..."
self.assertDictEqual(Dic1, Dic2, m)
    def test2(self):
        Dic1 = {"One": 1, "Two": 2}
        Dic2 = {"One": 1, "Two": 2}
        m = "Dictionaries are not equal ..."
self.assertDictEqual(Dic1, Dic2, m)
if __name__ == '__main__':
unittest.main()

This time, you will get the success message of test cases, i.e., Positive results.

Conclusion:

We have implemented the assertDictEqual method in Python to compare dictionaries. We have also seen a simple example of a similar function assertEqual() to make it more clear. Therefore, we have done it quite simply and easily to make you understand the concept of the assertDictEqual method.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content

Page was generated in 0.068952798843384