Test steps
1. Import the UnitTest module
Import UnitTest
2. Write the test class inheritance unittest. TestCase
Class Tester (UnitTest. TestCase)
3. Methods for writing tests must begin with test
def test_add (self)
def test_sub (self)
4. Test the function points using the method provided by the TestCase class
5. Call the Unittest.main () method to run all methods that begin with test
Copy the Code code as follows:
if __name__ = = ' __main__ ':
Unittest.main ()
Examples are as follows
The class being tested
Copy the Code code as follows:
#!/usr/bin/python
#coding =utf-8
Class computer (object):
@staticmethod
def add (A, B):
return a + B;
@staticmethod
Def sub (A, b):
return a-B;
Test class
Copy the Code code as follows:
#!/usr/bin/python
#coding =utf-8
Import UnitTest
From testee import computer
Class Tester (UnitTest. TestCase):
def test_add (self):
Self.assertequal (Computer.add (2, 3), 5, "Test add function")
def test_sub (self):
Self.assertequal (Computer.sub (5, 1), 4, "Test sub function")
if __name__ = = ' __main__ ':
Unittest.main ()
Operation Result:
Copy the Code code as follows:
----------------------------------------------------------------------
Ran 2 Tests in 0.000s
Ok