Unit Testing using Unittest in Python

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to learn about Unit Testing using the unittest built-in module. Testing plays a major role in software development. You will know the issues before going to the production itself.

We'll learn the basics of testing in Python using the built-in module called unittest. Let's jump into the tutorial.

What is Unit Testing?

If you take the login system as an example. Each field in the login form is a unit/component. And testing those units/components functionality is known as Unit Testing.

Example

Let's see the basic structure of unittest framework.

Live Demo

# importing unittest module
import unittest
# unittest will test all the methods whose name starts with 'test'
class SampleTest(unittest.TestCase):
# return True or False
def test(self):
self.assertTrue(True)
# running the test
unittest.main()

Output

If you run the above program, you will get the following results.

----------------------------------------------------------------------
Ran 1 test in 0.001s
OK

2.Testing String Methods

Now, we are going to test different string methods with sample test cases. Remember that the method name must start with test.

Let's see the brief intro about each method that we are going to write.

• test_string_equality

• This method tests whether two strings are equal or not using assertEqaul method of unittest.TestCase.

• test_string_case

• This method tests whether two string cases are equal or not using assertEqaul method of unittest.TestCase.

• test_is_string_upper

• This methods tests whether string is in upper case or not using assertTrue and assertFalse methods of unittest.TestCase .

Example

Live Demo

# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):
# string equal
def test_string_equality(self):
# if both arguments are equal then it's succes
self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
# comparing the two strings
def test_string_case(self):
# if both arguments are equal then it's succes
self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
# checking whether a string is upper or not
def test_is_string_upper(self):
# used to check whether the statement is True or False
# the result of expression inside the **assertTrue** must be True to pass the test case
# the result of expression inside the **assertFalse** must be False to pass the test case
self.assertTrue('TUTORIALSPOINT'.isupper())
self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()

Output

If you run the above code, then you will get the following result if all test cases are passes.

...
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK

Example

Run the following program to see the failed test case output.

Live Demo

# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):

# string equal
def test_string_equality(self):
# if both arguments are equal then it's succes
self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
# comparing the two strings
def test_string_case(self):
# if both arguments are equal then it's succes
self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
# checking whether a string is upper or not
def test_is_string_upper(self):
# used to check whether the statement is True or False
# the result of expression inside the **assertTrue** must be True to pass the test case
# the result of expression inside the **assertFalse** must be False to pass the test case
self.assertTrue('TUTORIALSPOINt'.isupper())
self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()

Output

If you run the above program, then you will get the following result.

======================================================================
FAIL: test_is_string_upper (__main__.TestingStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "p:/Python Work/Stopwatch/practice.py", line 21, in test_is_string_upper
self.assertTrue('TUTORIALSPOINt'.isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.016s
FAILED (failures=1)

We will fail message even one test case is failed among all the test cases.

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Published on 24-Apr-2020 12:11:22