Python is unittest unit comes with a test module, which is often used to do unit testing, which encapsulates it and the initialization operation performed by the embodiment, and returns the result of verification and other operations.
Before learning unittest framework will need to understand a few points of knowledge:
- TestCase test case
- TestSuit test set, i.e. a set of multiple test cases, referred TestSuit
- TestLoader loaded in the testcase to testsuit
- TestRunner is used to execute test cases, test results will be saved with the cases of the TestResult, including statistics after execution of all test cases, success or failure and so forth
Here by a concrete example to look at is how to achieve unittest
Import the unittest DEF CaCI2 (A, B): return A / B class the MyTest (of unittest.TestCase): # class names start with a capital needs DEF the setUp (Self): Print ( " before each use case will be executed execution " ) DEF the tearDown (Self): Print ( " after execution of each use case will be executed " ) @classmethod DEF setUpClass (CLS): Print ( " run before performing all the use cases ' ) @classmethod # class method DEF tearDownClass (CLS): Print ( " operation performed after all the use cases " ) DEF test_A (Self): res = cacl(1,2) self._testMethodDoc = " n Case " # display described when used to generate reports self.assertEqual (0.5, RES, ' Success ' ) # asserted DEF Test_B (Self): '' ' anti-Case ' '' # for generating report is the second display manner described in Example RES CaCI2 = (1,2 ) self.assertNotEqual(0.5, res, 'failed') if __name__ == '__main__': unittest.main () # can be run with all start with test cases, the order of execution in accordance with the order of Example ascii code value of the latter test, a smaller value of the first implementation
Here are some common assertions
assertEqual(a, b) a == b assertNotEqual(a, b) a != b assertTrue(x) bool(x) is True assertFalse(x) bool(x) is False assertIsNone(x) x is None assertIsNotNone(x) x is not None assertIn(a, b) a in b assertNotIn(a, b) a not in b
The above example is implemented in a way to be executed, how does it generate test reports, it is necessary to introduce another expansion pack download BeautifulReport pip source python module HtmlTestRunner or above, both modules need to install their own, the result of the test case execution and some statistical information will be logged to a html file
Installation HtmlTestRunner: HTMLTestRunner.py directly to the file in the directory environment variable in python to, any environment variable can be, lib, site-packages, etc.
Installation BeautifulReport: BeautifulReport folder into the site-packages directory, which is directory information because the underlying fixed frame
First create TestSuit test suite test suite
if __name__ == '__main__': test_suit = unittest.TestSuite () # Create a set with the embodiment Kit with Example # 1 embodiment test_suit.addTest (the MyTest ( " test_A " )) # The embodiment with a class added to the test set # mode 2 test_suit.addTest (unittest.makeSuite (MyTest)) # add MyTest in the test suite for all use cases # mode 3
# If we have a number of modules, each module below have written a lot of python files, each of which has a python file test cases, how to use cases in this directory have implemented it, we must first find all python files in this directory, and then find the inside of the test cases, one by one execution, the code is as follows:
all_case = unittest.defaultTestLoader.discover ( " case " , " *. Py " ) # get case directory (can be relative or absolute path or directory name), if the case becomes." "on behalf of all files in the current directory #All * .py all use cases under (support fuzzy matching) file [test_suit.addTests (Case) for Case in all_case] # loop all use cases added to the test suite
HTMLTestRunner generate test reports
Import HTMLTestRunner # introduction of expansion packs f = Open ( " test.html " , " wb " ) # need to open the file for writing binary form so you do not specify the character set before writing the report testrunner = HTMLTestRunner.HTMLTestRunner (stream = f, title = " test set title " , description = " test set described " ) testrunner.run (test_suit) # execute the test report and test report generation
BeautifulReport generate test reports (reports generated in this way more beautiful)
: Link resolve changes and report html page elements BeautifulReport underlying framework
Import BeautifulReport br AS # introduced package, after setting at alias br = br.BeautifulReport (test_suit) # be added to the object set with Example br.report (Description = " beautifulreport " , filename = " brtest " , log_path, = "" ) # execution cases statistical results, and generate test reports # filename is the name of the test report, log_path can specify the directory to store the test report, this parameter is optional
We follow continuous integration time, let the code to run automatically, Jenkins will be used, but the test reports are generated in html format above, do not know Jenkins, Jenkins is displayed in the inside out. That we must have some understanding of test reports Jenkins, Jenkins reported recognize xml format, then we chant generated xml format, you need to use a new module, xmlrunner, can be installed directly pip install xmlrunner, as follows:
import xmlrunner Runner = xmlrunner.XMLTestRunner (Output = ' Report ' ) # specific report discharge directory runner.run (test_suite)