Principle
Reference: Unit Test principle
Background
There was a period of time when the code was not written, so take advantage of a Python unit test for the weekend and test your Android app. Discover Pyunit while adding multiple test cases in a single script file, such as the method available on the website: official address
ImportUnitTestclasswidgettestcase (unittest. TestCase):defsetUp (self): Self.widget= Widget ("The widget") defTearDown (self): Self.widget.dispose () Self.widget=Nonedeftestdefaultsize (self):assertSelf.widget.size () = = (50,50),'Incorrect default size' deftestresize (self): Self.widget.resize (100,150) assertSelf.widget.size () = = (100,150), 'wrong size after resize'
Then combining the Htmltestrunner module, you can simply execute a test script and generate a test report, such as (after rectification):
However, there is a bad place, every time the unit test script will generate a report, so there is the idea of modifying unit tests (Add, log, batch execute all the scripts and synthesize a report)
Ideas
Viewing the Unit test report HTML source code, found that the increase, log easier, but to integrate the report of each unit test is more difficult for me, so use the following method:
1, directly inherit the existing test report, add on top, log view function
2. Traverse all unit test script name, class name, dynamic load script name + class name + method name into unit test collection
3, finally with the Htmltestrunner.htmltestrunner module Run method execution
Make up the increase
Based on the unit test flow, a save parameter is eventually added to the Htmltestrunner._generate_report_test () method
ImportCommonlibPrintCommonlib.screenshotpath Row= Tmpl%Dict (Tid=tid, Class= (n = = 0 and 'Hiddenrow' or 'None'), Style= N = = 2 and 'Errorcase' or(n = = 1 and 'Failcase' or 'None'), desc=Desc," "Parameters" "screenshot=Str (commonlib.screenshotpath), script=script, Status=Self . Status[n],) rows.append (row)if notHas_output:return
Add tags to the HTML template Report_test_with_output_tmpl used by the Htmltestrunner._generate_report_test () method. Method: View Unit test Report Source code, there is an error in the label, so in the same place to add tags
<href= "% (screenshot) S">screenshot</a ><href= "% (logcat) S">logdetail</ a>
Results such as the unit test report above. The method of adding logs should be about the same
Dynamic load unit testing relies on modules in two ways:
1, using importlib dynamic loading, but in the process of debugging found that the loop loading module will be reported in the module is not defined by the wrong, did not take half a day, so pass off
2. Use the file traversal method to find the unit test script name + class name + method name. For convenience, I define the name of each script to be the same as the class names, and all of the script's test methods are Teststep ()
Steps:
1. Traverse all scripts under the Unit test script path and record the script name
defget_file_name (path):" "@see: Traverse the. py file under the Unit test script file and get the. py filename @param path: Unit test script file path" "filenamelist= [] forRoot, dirs, filesinchOs.walk (path):ifFiles: forFiinchFiles:if 'Init' inchFior '. PYc' inchfi:Continue if '. PY' inchfi:fileNameList.append (Fi.split ('.') [0])returnFilenamelist
2. Write a single unit test script unittestdemo.py to add all testcase to Testsuite
if __name__=="__main__": Testsuite=UnitTest. TestSuite () path=Commonlib.unittestcasepath" "to add a single test case to the test set:" "Testdemo=add_testcase_to_suite (path) testsuite.addtests (Testdemo)defadd_testcase_to_suite (path):" "@see: Add a test Case collection" "FL=util.get_file_name (path) Testdemo= [] forFinchFl:ef= f +'.'+ F +'("Teststep")'testdemo.append (eval (EF))returnTestdemo
3, in the script unittestdemo.py dynamically add the first step in the name of the script obtained (that is, the dependent module name)
defconfig_unittest_demo_read (Path, moudlename):" "@see: Append unit Test script file module to unit test collection script @param path: Unit Test Collection script file path @param moudlename: module that the unit test script relies on" "FL=[] fo= Open (Path,'R') Try: forFinchfo.readlines ():if 'From Unittestdemo Import' inchf:p='from.+?\r'RC=Re.compile (p) Fr= F.replace (Rc.findall (f) [0], Moudlename +'\ r') Fl.append (FR)Continue Else: Fl.append (f)returnFLexceptioerror,e:PrintereturnFalsefinally: Fo.close ()
4. Execute all scripts using the Htmltestrunner.htmltestrunner Run method
" "generate a test report file" "file_name=Commonlib.unittestresultpath FP= File (file_name,'WB') " "Performing unit tests" "Renner=Htmltestrunner.htmltestrunner (Stream=FP, title='Test Results', Description='Test Report') Renner.run (testsuite)
Command line mode
For easy batch execution, you can also use the command line to see how the official command line runs. I'm running with the bat script here. First initialize all the dependencies of the unit test scripts to be executed, and then execute the unittestdemo.py
configUTDemo.bat
CD E:\Python27python E:\PythonProject\conifgUTDemo.py Ping 127.0. 0.1 >nul
startUTTest.bat
CD E:\Python27python E:\PythonProject\unitTestDemo.py Ping 127.0. 0.1 >nul
If necessary, it can be added to the scheduled task, scheduled execution
Python Unit Test Pyunit framework minor corrections