Whenever we use unittest, there are some methods we use in our script. These methods are the following:
- assertEqual() and assertNotEqual(): This checks for an expected result
- assertTrue() and assertFalse(): This verifies a condition
- assertRaises(): This verifies that a specific exception gets raised
- setUp() and tearDown(): This defines instructions that are executed before and after each test method
You can use the unittest module from the command line as well. So, you can run the previous test script as follows:
student@ubuntu:~$ python3 -m unittest test_addition.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
Now, we will see another example. We will create two scripts: if_example.py and test_if.py. if_example.py will be our normal script and test_if.py will contain test case. In this test, we...