Unit test unit testing framework can be applied not only to unit testing, but also to the development and execution of WEB automated test cases. The test framework can organize and execute test cases, and provide a rich assertion method to judge whether the test cases pass or not, and finally generate test results. The basic knowledge of python and unittest is not detailed. You can learn more about Baidu by yourself. Here are the four most important concepts of unittest.
test fixture
test case
test suite
test runner
test fixture: Simply speaking, it is to do something that needs to be prepared during the testing process, such as creating temporary databases, files and directories, among which setUp() and setDown() are the most commonly used methods.
Test case: User-defined base class of test case, calling run() method, will call setUP method, execution case method, tearDown () method in turn.
test suite: Test cases can be added manually by addTest() method or automatically by TestLoader. TestLoader has no order when adding use cases.
test runner: The driver class that runs the test case can execute either TestCase or TestSuite. TestCase and Testsuite automatically manage TestResult after execution.
Following is a sample code for simple unittest:
import unittest import HTMLTestRunner import time class TestCases(unittest.TestCase): def setUp(self): print("Each article case This method is executed before execution.") def tearDown(self): print("Each article case This method is executed after execution.\n") def test_testcase1(self): print("This is a test case case1") a = "hello" try: self.assertTrue(a.isalpha()) print("The test passed") except Exception as e: print("It's wrong. The result is wrong.%s" % e) print("failed") raise e def test_testcase2(self): print("This is a test case case2") b = "123" try: self.assertTrue(b.isalpha()) print("The test passed") except Exception as e: print("It's wrong. The result is wrong.%s" % e) print("failed") raise e def test_testcase3(self): print("This is a test case case3") c = " " try: self.assertTrue(c.isalpha()) print("The test passed") except Exception as e: print("It's wrong. The result is wrong.%s" % e) print("failed") raise e ''' if __name__ == "__main__": unittest.main() //Writing this code allows you to run model.py directly from the command line, and all use cases are executed. ''' """ //Loading test cases, not only in this way, but also in the way of using loader loader = unittest.TestLoader() suite.addTest(loader.loadTestsFromTestCase(TestCases)) or suite.addTest(loader.loadTestsFromModule(model)) """ suite = unittest.TestSuite() # suite.addTest("test_testcase2") # I made a mistake here. I need to add modules. suite.addTest(TestCases('test_testcase3')) """ //Specify the path of the test report and define the report name format """ report_dir = "../Test report" now = time.strftime("%Y-%m-%d %H-%M-%S") reportname = report_dir + "/" + now + " Test report.html" """ //Running use cases and generating test reports """ with open(reportname, "wb+") as file: runner = HTMLTestRunner.HTMLTestRunner(file, 2, title="Model test report", description="Hello testers! This is the description of Model test" "report") runner.run(suite)
unittest module summary:
As for this module, I didn't understand it very well when I studied this module by myself before, and these two days have been a little rewarding. When I did the example, I found that assertions could not be placed in try statements and could be included by except exception classes, otherwise the results of test cases could not be set by assertions.
Ruijiang Yun official website link: http://www.eflycloud.com/#register?salesID=6DGNUTUAV