Most write units in python use unittest and mock, and coverage is used to test code coverage. Then, use nose to concatenate all the things so that each time a book is published, all the unit tests of the entire project can be run once. Unittest unittest is not described in detail. Note the following points: the test class inherits unittest. the name of the TestCase test class and test method should start with test. Many tools can run automatically according to the name, so that setUp/tearDown in the test class will be executed before/after each case is executed, setUpClass/tearDownClass and @ classmethod add unittest to the main function that executes the test file at the beginning and end of the test class. main (), you can directly run the Mock unit test using the python command. The essence of the mock is Mock. Several common scenarios are introduced: 1. mock is a function. There are actually several methods. I personally recommend the following: copy the code def multiple (a, B): return a * B class TestProducer (unittest. testCase): def setUp (self): self. calculator = Calculator () @ mock. patch ('Multiple ') def test_multiple (self, mock_multiple): mock_multiple.return_value = 3 self. assertEqual (multiple (8, 14), 3) Copy Code 2. mock: The method in an object copies the code class Calculator (object): def add (self, a, B): return a + B class TestProducer (unittest. testCase ): Def setUp (self): self. calculator = Calculator () @ mock. patch. object (Calculator, 'add') def test_add (self, mock_add): mock_add.return_value = 3 self. assertEqual (self. calculator. add (8, 14), 3) Copy code 3. make the Mock function return different values each time it is called, and the method in 1 and 2 returns the same value each time it is called. Copy the code class TestProducer (unittest. testCase): @ mock. patch. object (Calculator, 'add') def test_effect (self, mock_add): mock_add.side_effect = [1, 2, 3] self. assertE Qual (self. calculator. add (8, 14), 1) self. assertEqual (self. calculator. add (8, 14), 2) self. assertEqual (self. calculator. add (8, 14), 3) Copy code 4. let the Mock function throw an exception and copy the code def is_error (self): try: OS. mkdir ("11") return False failed t Exception as e: return True class TestProducer (unittest. testCase): @ mock. patch ('OS. mkdir ') def test_exception (self, mock_mkdir): mock_mkdir.side_effect = Exception self. assertEq Ual (self. calculator. is_error (), True) Copy Code 5. multiple Mock functions are mainly used to copy the code in sequence @ mock. patch. object (Calculator, 'add') @ mock. patch ('test _ unit. multiple ') def test_both (self, mock_multiple, mock_add): mock_add.return_value = 1 mock_multiple.return_value = 2 self. assertEqual (self. calculator. add (8, 14), 1) self. assertEqual (multiple (8, 14), 2) copy the code Coverage and run the coverage command to add the test file. Then you can get the Coverage rate and generate reports in html format, each time a file is run,. coverag File e. You must extract all the results of combine to obtain a complete report. For specific command parameters see: http://nedbatchelder.com/code/coverage/cmd.html is more useful is the configuration file, see: http://nedbatchelder.com/code/coverage/config.html configuration file is the most useful function is can not test the coverage of some rows, for example: copy the Code [report] exclude_lines = # if you add the comment "# pragma: no cover" to a line, the line will be ignored. pragma: no cover # ignore the main function if _ name _ =. _ main __.: copy the code NoseNose to execute all unit test files at a time, and provide a coverage plug-in to measure the overall coverage rate. Nose scans the target directory. If the directory name starts with "test" or "Test", it recursively scans the directory, and automatically run all detected test files starting with "Test" or "test. In addition, Nose adds setup and teardown at the reporting level. You only need to put them in the _ init _. py file. The simplest way to execute the Nose command is to add the directory of all your test files or test files after nosetest. For some running parameters, see: the parameters in the http://nose.readthedocs.org/en/latest/usage.html Nose start with "-- cover" are coverage-related, but I found that there is no way to use the coverage configuration file, you need to manually install nose-cov, then use "-- cov-config" to specify the configuration file. For other parameters, see: https://pypi.python.org/pypi/nose-cov my project, which is troublesome because the test file is more dispersed and some do not start with test, only one script can be written to concatenate these scripts: copy the code import osimport subprocess ################################### #################################### test coverage file or directory cover_list = ['src/sample/analyzer/unpacker/src/emulator. py', 'src/sample/analyzer/unpacker/src/emulator_manager.py ', 'src/sample/analyzer/unpacker/src/unpacker_analyzer.py ', 'src/sample/analyzer/bitvalue/src/confparser. py', 'src/sample/analyzer/bitvalue/src/trunk. py',] # file or directory of the test case. If the test file does not start with test, you must specify the file name ut_list = ['src/sample/analyzer/unpacker/ut ', 'src/sample/analyzer/bitvalue/ut/ut_main.py '] ######################### ######################################## #### PRODUCTION_HOME = OS. environ. get ("PRODUCTION_HOME ",".. /.. ") def get_command (): command = ['nosetests', '-- with-cov', '-- cover-erase', '-- cov-report', 'html ', '-- cov-config', 'Cover. config ',] for cover in cover_list: command. append ('-- cov') command. append (OS. path. join (PRODUCTION_HOME, cover) for ut in ut_list: command. append (OS. path. join (PRODUCTION_HOME, ut) return command if _ name _ = '_ main _': command = get_command () print command OS. chdir (PRODUCTION_HOME) proc = subprocess. popen (command, shell = False, stdout = subprocess. PIPE, stderr = subprocess. PIPE) output, error = proc. communicate () return_code = proc. poll () print output print error print return_code