/ 开发  

What is unittest and how to write unit testing

Unit test is used for a module, a function or a class to test their validity.
We can write some test case for function abs() to test the validity of it:

1.Input a positive number: 1, 3.2 or 0.6. We expect the return value to be the same as the input.

2.Input a negative number: -1, -2,3 or -0.99. We expect the return value to be the opposite of the input.

3.Input 0. We expect to return 0.

4.Input non-numeric types, such as None, [], {}. We expect a TypeError to be thrown.

We put the above test cases into a test module, it’s a complete unit test.
If the unit test can be passed, which means that we tested this function to work properly.If the unit test does not pass, either the function has a bug, or the test condition is not entered correctly, we need fixes it to make unit tests pass.

What are the benefits of unit testing? Imagine if we modified the abs () source code, we only need to run the unit tests again, if the test passed it means our changes to the abs () function does not affect the original behavior. If the test does not pass, it means that our changes have problems, we need to modify the code or modify the test.

Next, we actually write code to illustrate the unit test.

abs.py

def abs(x):

    if not isinstance(x, (int, float)):
    raise TypeError('bad operand type')

    if x >= 0:
        return x
    else:
        return -x

This is a python file, which defines an abs () function that takes an absolute value.

Open Terminal, cd into the folder where the abs.py file is located. Input python and press Enter to enter python’s interactive programming environment:

$ cd /Users/hisoft/Desktop/Unittest
$ python
Python 2.7.11 (default, Jan 22 2016, 08:29:18) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Now imput the ‘import abs’ on the Termanal and press to enters the module name abs there.
We use abs.abs (-5) to invoking the function in the module to find the absolute value of -5.
This part of the python can refer to the official documents.

>>> import abs
>>> abs.abs(-5)
5    

abs_test.py

Now let’s write the unit test for the abs function. We use the Python’s unittest module, abs_test.py is like this:

import unittest

from abs import abs

class UnitTestDemo(unittest.TestCase):

    def setup(self):
        print('setUp...')

    def tearDown(self):
        print ('tearDoen...')

    def test_case1(self):

        self.assertEqual(abs(1), 1)
        self.assertEqual(abs(-1), 1)
        self.assertEqual(abs(0), 0)

    def test_case2(self):

        with self.assertRaises(TypeError):
            abs('1')

if __name__ == '__main__':
    unittest.main()

We need to write a test class When we write unit tests, This is inherited from ‘unittest.TestCase’.
The test method begins with test, There are test_case1 and test_case2. A method that does not start with test is not considered a test method and will not be executed when tested.

##Run the unit test
Now we can run unit tests. To the end of the abs_test.py with two lines of code:

if __name__ == '__main__':
    unittest.main()

The two lines of code means that this script has been completed,we can run it directly:

python abs_test.py

There are two methods in the unit test script setUp() and tearDown(), They will invoking before and after each test method execute.