例1:
mymytest.py 和main.py都在case文件夹中
在mymytest.py文件中
import unittest def two_he(a,b): print(a+b) return a+b class BdTestClass(unittest.TestCase): def test_1(self): two_he(1,2)
在main.py文件中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(mymytest.BdTestClass("test_1")) runner=unittest.TextTestRunner() runner.run(suite)
例2:
mymytest.py 和main.py都在case文件夹中
在mymytest.py文件中
import unittest # 待测函数 def two_he(a,b): print(a+b) return a+b # 测试类:方法-->测试用例 # 注意事项:测试类,必须继承unittest.TestCase # 测试用例:就是方法,方法必须以test开头 class BdTestClass(unittest.TestCase): def test_1(self): two_he(1,2) def test_2(self): two_he('abc',5)
在main.py文件中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(mymytest.BdTestClass("test_1")) suite.addTest(mymytest.BdTestClass("test_2")) runner=unittest.TextTestRunner() runner.run(suite)
例3:
mymytest.py 和main.py都在case文件夹中
在mymytest.py文件中
import unittest def two_he(a,b): print(a+b) return a+b class BdTestClass(unittest.TestCase): def test_1(self): two_he(1,2) def test_2(self): two_he('abc',5)
在main.py文件中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(mymytest.BdTestClass)) runner=unittest.TextTestRunner() runner.run(suite)
和例2效果一样
例4:
测试一个文件夹下的所有要测的文件
mymytest.py 和yotest.py都在case文件夹中
main.py文件在case文件夹之外(和cace同级)
在mymytest.py文件中
import unittest def two_he(a,b): print(a+b) return a+b class BdTestClass(unittest.TestCase): def test_1(self): two_he(1,2) def test_2(self): two_he('abc',5)
在yotest.py文件中
import unittest def th_he(a,b,c): print(a+b) return a+b class ShTestClass(unittest.TestCase): def test_1(self): th_he(1,2,3) def test_2(self): th_he('abc',5,6)
在main.py文件中
import unittest
suite=unittest.TestLoader().discover('../case','*test.py') runner=unittest.TextTestRunner() runner.run(suite)
yotest.py和mymytest.py文件都测试
2.Fixture
例
mymytest.py 在case文件夹中
在mymytest.py中
import unittest def two_he(a,b): return a+b class BdTestClass(unittest.TestCase): def setUp(self) -> None: print('setup开始执行') def tearDown(self) -> None: print('teardown开始执行') def test_1(self): two_he(1,2) print('test1') def test_2(self): two_he(9,5) print('test2')
main.py文件与case文件夹同级
在main.py 中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(mymytest.BdTestClass)) runner=unittest.TextTestRunner() runner.run(suite)
例
mymytest.py 在case文件夹中
在mymytest.py中
import unittest def two_he(a,b): return a+b class BdTestClass(unittest.TestCase): @classmethod def setUpClass(cls) -> None: print('setupclass开始执行') @classmethod def tearDownClass(cls) -> None: print('teardownclass开始执行') def test_1(self): two_he(1,2) print('test1') def test_2(self): two_he(9,5) print('test2')
main.py文件与case文件夹同级
在main.py 中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(mymytest.BdTestClass)) runner=unittest.TextTestRunner() runner.run(suite)
3.断言,参数化与跳过操作
例
mymytest.py 在case文件夹中
在mymytest.py中
import unittest def two_he(a,b): return a+b class BdTestClass(unittest.TestCase): def test_1(self): ret=two_he(1,2) self.assertEqual(3,ret,msg='不等于3') def test_2(self): ret=two_he(9,5) self.assertEqual(12,ret,msg='不等于12')
main.py文件与case文件夹同级
在main.py 中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(mymytest.BdTestClass)) runner=unittest.TextTestRunner() runner.run(suite)
参数化
安装:pip install parameterized
例
mymytest.py 在case文件夹中
在mymytest.py中
from parameterized import parameterized import unittest def two_he(a,b): return a+b class BdTestClass(unittest.TestCase): @parameterized.expand([(1,2,3),(2,2,3),(3,2,5),(0,2,2)]) def test_1(self,x,y,exp): ret=two_he(x,y) self.assertEqual(exp,ret,msg='不相等')
main.py文件与case文件夹同级
在main.py 中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(mymytest.BdTestClass)) runner=unittest.TextTestRunner() runner.run(suite)
例
建立data.json文件,与case文件夹同级,
在data.json中
[ {"x": 1,"y": 2,"exp": 3}, {"x": 1,"y": 3,"exp": 4}, {"x": 1,"y": 2,"exp": 5}, {"x": 2,"y": 2,"exp": 4} ]
mymytest.py 在case文件夹中
在mymytest.py中
import json from parameterized import parameterized import unittest def two_he(a,b): return a+b def build_data(): with open('./data.json','r',encoding='utf-8')as f: # 注意/data.json前面的点,有的地方是两个点 content=f.read() d=[(i["x"],i["y"],i["exp"]) for i in json.loads(content)] return d class BdTestClass(unittest.TestCase): @parameterized.expand(build_data) # build_data() 也可以 def test_1(self,x,y,exp): ret=two_he(x,y) self.assertEqual(exp,ret,msg='不相等')
main.py文件与case文件夹同级
在main.py 中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(mymytest.BdTestClass)) runner=unittest.TextTestRunner() runner.run(suite)
跳过用例
例
建立data.json文件,与case文件夹同级,
在data.json中
[ {"x": 1,"y": 2,"exp": 3}, {"x": 1,"y": 3,"exp": 4}, {"x": 1,"y": 2,"exp": 5}, {"x": 2,"y": 2,"exp": 4} ]
mymytest.py 在case文件夹中
在mymytest.py中
import json from parameterized import parameterized import unittest def two_he(a,b): return a+b def build_data(): with open('./data.json','r',encoding='utf-8')as f: content=f.read() d=[(i["x"],i["y"],i["exp"]) for i in json.loads(content)] return d class BdTestClass(unittest.TestCase): @parameterized.expand(build_data()) def test_1(self,x,y,exp): ret=two_he(x,y) self.assertEqual(exp,ret,msg='不相等') @unittest.skip('不执行') def test_2(self): ret=two_he(1,9) self.assertEqual(12,ret,msg='不相等')
main.py文件与case文件夹同级
在main.py 中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(mymytest.BdTestClass)) runner=unittest.TextTestRunner() runner.run(suite)
例
建立data.json文件,与case文件夹同级,
在data.json中
[ {"x": 1,"y": 2,"exp": 3}, {"x": 1,"y": 3,"exp": 4}, {"x": 1,"y": 2,"exp": 5}, {"x": 2,"y": 2,"exp": 4} ]
mymytest.py 在case文件夹中
在mymytest.py中
import json from parameterized import parameterized import unittest v=1.0 def two_he(a,b): return a+b def build_data(): with open('./data.json','r',encoding='utf-8')as f: content=f.read() d=[(i["x"],i["y"],i["exp"]) for i in json.loads(content)] return d class BdTestClass(unittest.TestCase): @parameterized.expand(build_data()) def test_1(self,x,y,exp): ret=two_he(x,y) self.assertEqual(exp,ret,msg='不相等') @unittest.skipIf(v==1.0,'不执行') # v==1.0会跳过,等于其他的就不会跳过 def test_2(self): ret=two_he(1,9) self.assertEqual(12,ret,msg='不相等')
main.py文件与case文件夹同级
在main.py 中
import unittest from case import mymytest suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(mymytest.BdTestClass)) runner=unittest.TextTestRunner() runner.run(suite)
4.生成HTML测试报告
建一个py文件,与case文件夹同级,名字是HTMLTestRunner.py,把下面的代码粘贴进去
""" A TestRunner for use with the Python unit testing framework. It generates 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 Tung All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * 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 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 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, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ # URL: http://tungwaiyip.info/software/HTMLTestRunner.html __author__ = "Wai Yip Tung" __version__ = "0.8.2" """ Change History Version 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