Do the development of friends in writing code at the same time generally will write test code, which for the students to do operation and maintenance is seldom used.
Today we will write the test code, using the UnitTest module.
Cat test.py
Import UnitTest
def isodd (n):
return n% 2 = = 1
Class Isoddtests (UnitTest. TestCase):
def setUp (self):
Print "Test start ....."---"
def tearDown (self):
Print "Test end ......"
def testone (self):
Self.failunless (IsOdd (2))
def testtwo (self):
Self.failif (IsOdd (2))
def testcase1 (self):
Self.version = "1.6.1"
Self.assertequal (Self.version, "2.2", "version should be 1.6.1")
if __name__ = = ' __main__ ':
Unittest.main ()
For UnitTest the general usage is to put unittest. TestCase is passed as a subclass to a new class, and then the test function is written inside the class.
Note that the test function name must start with "test" or it will not be executed.
"SetUp" and "TearDown" are functions that request and reclaim resources.
Follow up on this blog post and stay tuned for further updates.
This article is from the "Linux operation and Maintenance" blog, reproduced please contact the author!
Python unittest writing test code