拉钩4.2-unittest测试框架
1、单元测试的用例设计方法
语句覆盖
条件覆盖
判断覆盖
路径覆盖
2、unittest
https://docs.python.org/3/library/unittest.html
1)执行unittest的3种方式:最好直接使用python run.py -v等方式执行,因为IDE有bug,会有历史执行结果
2)实例
# 导入inittest模块 import unittest # 类名称要Test开头,且继承unittest.TestCase class TestStringMethods(unittest.TestCase): # 在每个测试用例的前后调用setUp、tearDown,默认返回值为None def setUp(self) -> None: print('setup----如数据库连接,登录等操作') def tearDown(self) -> None: print('teardown----数据库断连,登出等操作') # 在每个类的前后调用setUpClass、tearDownClass @classmethod def setUpClass(cls) -> None: print('setUpClass') @classmethod def tearDownClass(cls) -> None: print('tearDownClass') # 方法名称要test_开头 def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') print('用例执行test_upper') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) print('用例执行test_isupper') def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) print('用例执行test_split') if __name__ == '__main__': unittest.main()
import unittest # 被测试方法 class Search(): def search_fuc(self): print('被测试方法search_fuc') return True # 测试类1 class TestSearch(unittest.TestCase): @classmethod def setUpClass(cls) -> None: print('setup class') # 实例化Search类,定义类变量则需要用cls cls.search = Search() @classmethod def tearDownClass(cls) -> None: print('teardown class') def test_search1(self): assert True == self.search.search_fuc() print('test_search1') def test_search2(self): assert True == self.search.search_fuc() self.assertEqual(1,1,'断言1==1') self.assertNotEqual(1,1,'断言1!=1') self.assertTrue(1==1,'断言1==1表达式结果是True') self.assertFalse(1==1,'断言1==1表达式结果是False') print('test_search2') def test_equal(self): self.assertEqual(1,1,'断言1==1') def test_noptequal(self): self.assertNotEqual(1,1,'断言1!=1') def test_true(self): self.assertTrue(1==1,'断言1==1表达式结果是True') def test_false(self): self.assertFalse(1==1,'断言1==1表达式结果是False') # 测试类2 class TestSearch2(unittest.TestCase): @classmethod def setUpClass(cls) -> None: print('setup class222222222222') # 实例化Search类,定义类变量则需要用cls cls.search = Search() @classmethod def tearDownClass(cls) -> None: print('teardown class2222222222') def test_search11111(self): assert True == self.search.search_fuc() print('test_search11111') @unittest.skip('这次不想执行这条用例') def test_search22222(self): assert True == self.search.search_fuc() print('test_search22222') class TestSearch3(unittest.TestCase): @classmethod def setUpClass(cls) -> None: print('setup class3333333333333333') # 实例化Search类,定义类变量则需要用cls cls.search = Search() @classmethod def tearDownClass(cls) -> None: print('teardown class333333333333') def test_search33333(self): assert True == self.search.search_fuc() print('test_search33333') @unittest.skip('这次不想执行这条用例') def test_search44444(self): assert True == self.search.search_fuc() print('test_search44444') # if __name__ == '__main__': # 方法一:运行当前模块所有以test_开头的测试用例 # unittest.main() # 方法二:执行指定的测试用例,创建一个测试套件,将某几条测试用例加入到测试套件中,批量执行 # suite = unittest.TestSuite() # suite.addTest(TestSearch('test_search1')) # suite.addTest(TestSearch('test_search2')) # unittest.TextTestRunner().run(suite) # 方法三:执行指定的测试类 # suite1 = unittest.TestLoader().loadTestsFromTestCase(TestSearch2) # suite2 = unittest.TestLoader().loadTestsFromTestCase(TestSearch3) # suite = unittest.TestSuite([suite1,suite2]) # unittest.TextTestRunner(verbosity=2).run(suite) # # 方法三:运行某个目录下所有匹配pattarn的测试文件,测试用例都放到 testcase目录,外层放一个 run.py文件,内容如下 # import unittest # # if __name__ == '__main__': # testdir = './testcases' # discover = unittest.defaultTestLoader.discover(testdir, pattern='test_*.py') # unittest.TextTestRunner(verbosity=2).run(discover)
3、生成测试报告
https://github.com/huilansame/HTMLTestRunner_PY3
以下是util执行用例并形成报告的写法实例
import unittest import util.HTMLTestRunner_PY3 if __name__ == '__main__': testdir = './testcases' discover = unittest.defaultTestLoader.discover(testdir, pattern ='test_*.py') # unittest.TextTestRunner(verbosity=2).run(discover) report_title = 'Example用例执行报告' desc = '用于展示修改样式后的HTMLTestRunner' report_file = 'utilReport.html' with open(report_file, 'wb') as report: runner = util.HTMLTestRunner_PY3.HTMLTestRunner(stream=report, title=report_title, description=desc) runner.run(discover)
注意,这里还是要使用python run.py来执行
报告内容如下