Python使用TestLoader、TestSuite、HTMLTestRunner组织测试用例实例(htmltestrunner生成测试报告)
Python使用TestLoader、TestSuite、HTMLTestRunner组织测试用例实例(htmltestrunner生成测试报告)
HTMLTestRunner.py:'''unittest提供一个TestLoader类用于自动创建一个测试集并把单个测试放入到测试集中。TestLoader自动运行测试用例以test开头的方法的测试方法。在多个测试用例在决定运行哪个测试用例的策略是通过内建函数对测试函数名排序决定的。通常习惯将测试集组合在一起,以便系统一次运行所有的测试用例。TestSuite实例添加到一个TestSuite中就像把一个TestCase实例添加到一个TestSuite中。-----------------------------------------------------------------------------------------suite1=module1.TheTestSuite()suite2=module2.TheTestSuite()alltests=unittest.TestSuite([suite1,suite2])-----------------------------------------------------------------------------------------可以将测试用例和测试套件的定义放在与要测试的代码相同的模块中,但将测试代码放在单独的模块中有几个好处:1、测试模块可以从命令行独立运行。2、测试代码可以更容易地与运输代码分开。3、有更少的诱惑改变测试代码,以适应代码测试没有一个很好的理由。4、测试代码的修改频率要比它测试的代码少得多。5、测试代码可以更容易重构。6、用C编写的模块的测试必须在单独的模块中,所以为什么不一致?7、如果测试策略更改,则不需要更改源代码。'''from __future__ import divisionfrom Lib.HTMLTestRunner import HTMLTestRunnerfrom unittest import TestCase,TestLoader,TestSuitefrom source.calcutor import calculatorClassclass TestMul(TestCase): def setUp(self): pass def test_defaultMul(self): cal=calculatorClass() self.assertEqual(cal.mul(),200 , "The result should be equal 200") def test_negtiveMul(self): cal=calculatorClass(-10,-25) self.assertEqual(cal.mul(),250 , "The result should be equal 250") def test_floatMul(self): cal=calculatorClass(0.25,0.4) self.assertEqual(cal.mul(),0.1, "The result should be equal 0.1") def tearDown(self): pass class TestDiv(TestCase): def setUp(self): pass def test_defaultDiv(self): cal=calculatorClass() self.assertEqual(cal.mul(),0.5 , "The result should be equal 0.5") def test_negtiveDiv(self): cal=calculatorClass(-10,-25) self.assertEqual(cal.mul(),0.4 , "The result should be equal 250") def test_floatDiv(self): cal=calculatorClass(0.24,0.4) self.assertEqual(cal.mul(),0.6, "The result should be equal 0.1") def tearDown(self): pass def allTest(): ''' 创建测试集''' suite1=TestLoader().loadTestsFromTestCase(TestMul) suite2=TestLoader().loadTestsFromTestCase(TestDiv) alltests=TestSuite([suite1,suite2]) return alltestsif __name__=="__main__": '''创建保存测试结果的文件''' html=file("..\\report.html","wb+") '''调用HTMLTestRunner类生成html格式测试运行报告''' runner=HTMLTestRunner(stream=html,title="Test Report",description="The state of the run testcase") runner.run(allTest()) html.close()
测试运行结果报告如下:
附录:
HTMLTestRunner.py
"""A TestRunner for use with the Python unit testing framework. Itgenerates a HTML report to show the result at a glance.The simplest way to use this is to invoke its main method. E.g. import unittest import HTMLTestRunner ... define your tests ... if __name__ == '__main__': HTMLTestRunner.main()For more customization options, instantiates a HTMLTestRunner object.HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g. # output to a file fp = file('my_report.html', 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='My unit test', description='This demonstrates the report output by HTMLTestRunner.' ) # Use an external stylesheet. # See the Template_mixin class for more customizable options runner.STYLESHEET_TMPL = '' # run the test runner.run(my_test_suite)------------------------------------------------------------------------Copyright (c) 2004-2007, Wai Yip TungAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* Neither the name Wai Yip Tung nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITEDTO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR APARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNEROR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""# URL: = "Wai Yip Tung"__version__ = "0.8.2""""Change HistoryVersion 0.8.2* Show output inline instead of popup window (Viorel Lupu).Version in 0.8.1* Validated XHTML (Wolfgang Borgert).* Added description of test classes and test cases.Version in 0.8.0* Define Template_mixin class for customization.* Workaround a IE 6 bug that it does not treat %(heading)s%(report)s%(ending)s