The Python version of the UnitTest testing framework is very useful. Although I have seen all the complete functions, I found that there are only a few commonly used in daily work. Began to arrange, forget a lot of, remember the side of the side to come in.
-
Skip (“) unittest.skip(“)
-
Skip the use case unittest.skpiif (3>2) for true
-
Actively flag as Failure unittest.failure (” “)
-
A function method that automatically reruns after a use case fails
def log(n):
def decorator(xingfangfa):
def wrapper(*a,**w):
for i in range(n):
try:
return xingfangfa(*a,**w)
except AssertionError as err:
print err
raise AssertionError
return wrapper
return decorator5. There are many ways to start the test suite in the main function
1. If the name = = “main” : Suite = unittest.testSuite () suite. AddTest (TestCount(” test_add “)) Runner = unittest.texttestrunner () runner. Run (suite) TextTestRunner() runner = unittest.makesuite () Runner = unittest.texttestrunner () runner. Run () 3. Def suite(suiten): Suitea = suiten for I in range(0,3): suitea.addTest(TestNut(‘ test_%s’ %(I +1))) return suitea if name == “main” : suiten = unittest.TestSuite() runner = unittest.TextTestRunner() runner.run(suite)
6. If an assertion fails in each use case, the following code is not executed, so if some action code is needed after the assertion, it is best to put it in the next use case. You can have many test classes, just add suiten to the main function in order to make debugging easier
suiten = unittest.makeSuite(Test_phonelogin)
suiten2= unittest.makeSuite(Test_emaillogin)
suiten.addTest(suiten2)
suiten.addTest(suiten3)
8. Introducing HMLTEstrunner is a good choice. However, all Chinese strings in the code need to be preceded by u
Filename = ‘. / new/login page test. HTML ‘fp = file (filename,’ wb) runner = HTMLTestRunner. HTMLTestRunner (fp, title = u registered login module test, Description = U ‘NutApp use-case execution report ‘) Runner. Run (Suiten)