Unit Testing is one of the best practice software development process.
Unit tests are written to detect bugs early in the development of the application when bugs are less frequent and less expensive to fix.
The purpose of writing unit test is to detect bugs, handle hidden test cases or certain input which is not taken care while writing code or program and write bug free code.
A unit is the smallest testable part of any program. It usually has one or a few inputs and usually a single output.
Here i am going cover the basics of how to create and run a simple test using unittest in python programming.
Let's Create a basic example
The unittest module provides a rich set of tools for constructing and running tests.
This post will demonstrate how to write test cases for small and large program in python to meet the needs of most users.
We are going to write test case for add function
Example 1: Single test case for add function
import unittest
#add function
def add(a,b):
return a + b
class AddTest(unittest.TestCase):
def test(self):
self.assertEqual(add(3,4), 7)
if __name__ == '__main__':
unittest.main()
Output:
..
----------------------------------------------------------------------
Ran 1 tests in 0.000s
OK
Example 2: Multiple Test case for add function
import unittest
#add function
def add(a,b):
return a + b
class AddTest(unittest.TestCase):
def test1(self):
self.assertEqual(add(3,4), 7)
def test2(self):
self.assertEqual(add(7,4), 11)
if __name__ == '__main__':
unittest.main()
Output:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Example 3: Here is a short script to test three string methods:
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
Output:
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
Recommendation:
A testcase is created by subclassing unittest.TestCase.
The three individual tests are defined with methods whose names start with the letters test.
This naming convention informs the test runner about which methods represent tests.
Use of these three methods:
assertEqual() : to check for an expected result;
assertTrue() or assertFalse() : to verify a condition;
assertRaises() : to verify that a specific exception gets raised.
These methods are used instead of the assert statement so the test runner can accumulate all test results and produce a report.
The setUp() and tearDown() methods allow you to define instructions that will be executed before and after each test method. They are covered in more detail in the section Organizing test code.
The final block shows a simple way to run the tests. unittest.main() provides a command-line interface to the test script.
When run from the command line, the above script produces an output that looks like this:
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
For more information about unittest you can go through python official website
unittest — Unit testing framework
List of Assignment Expert Services by Codersarts
Computer science Assignment help