Python the same file, there is unittest do not execute "if __name__ = = ' __main__", do not generate Htmltestrunner test report Solutions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.