Python Unit
Python Unit
Python contains many test runners. The most popular build-in Python library is called unittest.
The unittest is portable to the other frameworks. Consider the following three top most test
runners.
unittest
nose Or nose2
pytest
We can choose any of them according to our requirements. Let's have a brief introduction.
unittest
The unittest is built into the Python standard library since 2.1. The best thing about the unittest,
it comes with both a test framework and a test runner. There are few requirements of the
unittest to write and execute the code.
Python Unit-testing:
Unit testing is a software testing method in which individual components of the program, called units,
are tested independently with all the required dependencies. Unit testing is mostly done by the actual
programmers, who write the programs for the units. In smaller projects, it is done informally. In most of
the very large-scale projects, unit testing is part of a formal process of development with proper
documentation and proper schedule/ efforts allocated to it.
Python unittest module is used to test a unit of source code. Suppose, you need to test your project. You
know what kind of data the function will return. After writing huge code, you need to check it whether
the output is correct or not.
Normally, what we do is printing the output and match it with the reference output file or check the
output manually.
OK: If all test cases are passed, the output shows OK.
Failure: If any of test cases failed and raised an Assertion Error exception
Error: If any exception other than Assertion Error exception is raised. There are several function under
unittest module. They are listed below
METHOD CHECKS THAT
assertEqual(a,b) a==b
assertNotEqual(a,b) a != b
assertIs(a,b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
import unittest
def fun(x):
return x + 1
class MyTest(unittest.TestCase):
def test(self):
self.assertEqual(fun(3), 4)
if __name__ == "__main__":
unittest.main()
if __name__ == "__main__":
unittest.main()
This allows us to run all of the test code just by running the file.
import unittest
class TestClass01(unittest.TestCase):
def test_case01(self):
my_int = 999
self.assertTrue(isinstance(my_str, str))
self.assertTrue(isinstance(my_int, int))
def test_case02(self):
my_pi = 3.14
self.assertFalse(isinstance(my_pi, int))
if __name__ == '__main__':
unittest.main()
Up until now, you have run the test modules with unittest.main(). Now you will see how to run the test
module without unittest.main()
import unittest
class TestClass07(unittest.TestCase):
def test_case01(self):
self.assertTrue("PYTHON".isupper())
print("\nIn test_case01()")
import unittest
class TestingSum(unittest.TestCase):
def test_sum(self):
def test_sum_tuple(self):
if __name__ == '__main__':
unittest.main()
Here we will apply all the concepts that we have learned in earlier section. First, we need to
create a file name test.py or anything. Then make inputs and execute the code being tested,
capturing the output. After successfully run the code, match the output with an expected result.
First, we create the file my_sum file and write code in it.
def sum(arg):
total = 0
total += val
return total
import unittest
class CheckSum(unittest.TestCase):
def test_list_int(self):
data = [1, 2, 3]
result = sum(data)
self.assertEqual(result, 6)
if __name__ == '__main__':
unittest.main()
After running the code, it returns dot(.) which means there is no error in the code.