Test steps
1. Import UnitTest Module
Import UnitTest
2. Classes that write tests inherit unittest. TestCase
Class Tester (UnitTest. TestCase)
3. The method of writing tests must begin with test
def test_add (self)
def test_sub (self)
4. Use the method provided by TestCase class to test the function point
5. Invoke the Unittest.main () method to run all methods beginning with test
Copy Code code as follows:
if __name__ = = ' __main__ ':
Unittest.main ()
Examples are as follows
tested Class
Copy 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;<strong> </strong>
Test class
Copy 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 ()
Run Result:
Copy Code code as follows:
----------------------------------------------------------------------
Ran 2 Tests in 0.000s
Ok