Python

Python AssertEqual Method

The assertEqual() is indeed a “unittest” utility method in Python that has been castoff to verify the equivalence of two possible values during unit testing. The method would accept three arguments as inputs and, based upon that assert circumstance, would generate a boolean expression. If the two input variables, strings, or values are equivalent, assertEqual() returns true; otherwise, it returns false. Within this guide, we will be looking at using the “assertEqual” method on some values while working in Python using Spyder3 for this purpose.

Example 01:

Let’s have our first example of utilizing the assertEqual method in the python code to see a positive test case. Within the code area, you need to import the “unittest” library of Python at the starting position, as you can see from the picture. The “If” statement is utilized here to check if the name has been found as “__main__,” it will call the main() function constructor using the unittest() main function call. The built-in class “TestStringMethods” of unittest has been utilized to test the test cases.

The result of test cases would be positive or negative at the end. It would be best to use the “TestCase” built-in module utilizing the unittest package within the TestStringMethods class. Here are the user-defined functions to test different conditions. As demonstrated, we have defined the new function with the keyword “def” and given it the name “test_split.” This newly created user-defined function utilizes the assertEqual function to check whether two given values are equal or not. The first value is a mixed string of characters and “-“ dashes.

The split() function has been applied to this string value to split the function on each “-“ character, i.e., ignoring the “-. “ The other value is a list of three characters, i.e., x, y, z. So, the assertEqual function will check the value at the first place of a string with the value at the first place of a list. If the values are matched, it will display the test case result on the shell. As you can see, the values are the same ignoring the “-“ sign. So, it must give us a positive result.

import unittest
class TestStringMethods(unittest.TestCase):
    def test_split(self):
self.assertEqual("x-y-z".split("-"), ["x", "y", "z"])
if __name__ == '__main__':
unittest.main()

After running this python code in Spyder3, we have got the below positive result for our assertEqual method use on two values. The test has been completed in 0.001s, and the “OK” message affirms that the values were equal.

Example 02:

Let’s take a look at another similar example. This time we will utilize two functions to test two test cases separately. Thus, we have started our python code again by importing the unit test library. The if statement is utilized likewise to check for the main() function. If it matches, the unittest library will call its main() function, and the execution begins. The built-in “TestStringMethods” class is declared using the “TestCase” module in the code via the unittest package in parameter.

The first user-defined function named “test_Split” is used to check if two values are equal or not using the split function on the 1st value. The assertEqual function is applied to two different conditions here. The other user-defined function “test_count” is used to check if the given string has a total of numbers of characters “W” or not.

import unittest
class TestStringMethods(unittest.TestCase):
    def test_split(self):
self.assertEqual("x-y-z".split("-"), ["x", "y", "z"])
self.assertEqual("d-e-f".split("-"), ["d", "e", "f"])
    def test_count(self):
self.assertEqual("WillaimWordsWorth".count("W"), 2)
if __name__ == '__main__':
unittest.main()

Upon code run, we have got the negative result, i.e., FAILED test case. Two tests for the “split” function were successful, while 1 test for the count function was failed.

To get the positive result, we have replaced 2 with 3 in the test_count function.

import unittest
class TestStringMethods(unittest.TestCase):
    def test_split(self):
self.assertEqual("x-y-z".split("-"), ["x", "y", "z"])
self.assertEqual("d-e-f".split("-"), ["d", "e", "f"])
    def test_count(self):
self.assertEqual("WillaimWordsWorth".count("W"), 3)
if __name__ == '__main__':
unittest.main()

In return, the test cases for both functions are positive, i.e., successful.

Example 03:

Here is another example of using the assertEqual method in the code. This time we are using a single test case function. The code’s libraries, main method, class, and modules are utilized the same as before. The only change has been made in the “test” function. We have declared and initialized two variables, v1 and v2, with different string values. Variable “m” shows some long string value, stating that values are not equal. The assert equal function is now used to check whether the variable v1 value is equal to or similar to the variable v2 value and display the consecutive message using the “m” string.

import unittest
class TestStringMethods(unittest.TestCase):
    def test(self):
        v1 = "hello"
        v2 = "python"
        m = "First value and second value are not equal !"
self.assertEqual(v1, v2, m)
if __name__ == '__main__':
unittest.main()

The execution shows that the values for v1 and v2 are not equal, i.e., test case FAILED.

We have updated our code to make this test case positive and successful. You need to update the value of one of the variables, i.e., we have replaced the v1 value from “hello” to “python.”

import unittest
class TestStringMethods(unittest.TestCase):
    def test(self):
        v1 = "python"
        v2 = "python"
        m = "First value and second value are equal !"
self.assertEqual(v1, v2, m)
if __name__ == '__main__':
unittest.main()

In return, our test case has been successful using the assertEqual function example.

Conclusion:

So, this was all about python’s assertEqual method to check the equality of two values in a unit test. We believe that we have done a great job in this tutorial, and you will find it helpful in any case.

About the author

Kalsoom Bibi

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