1. Problem: After using both the UnitTest framework and the Htmlreport framework in the same. py file in Python, Htmlreport is not executed.
2, why? actually not htmlreport not be executed, also not htmlreport not generate test report, is because if __name__ = = ' __main__ ' code at all did not execute well!
3. The source of the solution: because I didn't write print statements in my main code at first. Did not generate HTML report, I also looked for a long time on the Internet method, later only to suspect that is not running the main method, so wrote a print statement, sure enough not run. So I looked for the Python unittest to run the way, finally found a solution, now share to everyone.
Example code:
1 ImportUnitTest2 ImportHtmltestrunner3 4 classdemotest (unittest. TestCase):5 6 defTest_one (self):7 Print('First case')8 defTest_two (self):9 Print('Article two case')Ten One if __name__=='__main__': A Print("Start main") -Suite =UnitTest. TestSuite () -Suite.addtest (Demotest ('Test_one')) theSuite.addtest (Demotest ('Test_two')) - -filename ='e:\\test.html' -fp = open (filename,'W') + -Runner = Htmltestrunner.htmltestrunner (STREAM=FP, output='e:/', report_title='Test-results', +Descriptions=u'First Python unittest') A Runner.run (Suite) at -Fp.close ()
Run:
The console displays the results as above, and does not print out "start main". for what? is because the way of operation is not right.
1. Open edit configuration. Such as:
2, see how to run, there are two kinds of python and python tests. For example, the first method is actually the same as executing python123.py in CMD.
3, manually add Python run mode. such as: Remember Dot ok~~~~ write missed a step
3, the direct operation is good.
4, go to the e-disk to find the generated HTML report
All right, ~~bye bye~~.
Python the same file, there is unittest do not execute "if __name__ = = ' __main__", do not generate Htmltestrunner test report Solutions