系统学习Python——单元测试unittest:内置装饰器

网友投稿 327 2022-08-30


系统学习Python——单元测试unittest:内置装饰器

在自动化测试过程中,我们可能会遇到这样的场景:在某些情况下,测试用例虽然不需要执行,但是我们又不愿意删掉它。下面来看看​​unittest​​提供的装饰器功能。

无条件跳过装饰器

下面的代码借助​​@unittest.skip('skip info')​​装饰器,演示无条件跳过执行某个方法:

import unittestclass MyTestClassOne(unittest.TestCase): @classmethod def setUpClass(cls): print('setUpClass') def setUp(self): print('setUp') def test_001(self): print('test_first') self.assertEqual(0, 0) @unittest.skip('skip info') def test_002(self): print('test_second') self.assertEqual(0, 0) def test_003(self): print('test_third') self.assertEqual(0, 0) def test_004(self): print('test_fourth') self.assertEqual(0, 0) def tearDown(self): print('tearDown') @classmethod def tearDownClass(cls): print('tearDownClass')

运行结果如下:

============================= test session starts =============================collecting ... collected 4 itemstestclass.py::MyTestClassOne::test_001 setUpClasssetUptest_firsttearDowntestclass.py::MyTestClassOne::test_002 SKIPPED (skip info) Skipped: skip infotestclass.py::MyTestClassOne::test_003setUptest_thirdtearDowntestclass.py::MyTestClassOne::test_004setUptest_fourthtearDowntearDownClass======================== 3 passed, 1 skipped in 0.03s =========================Process finished with exit code 0

满足条件跳过装饰器

下面的代码借助​​@unittest.skipIf(condition, 'info')​​装饰器,演示当满足某个条件时,跳过执行某个方法:

import unittestclass MyTestClassOne(unittest.TestCase): i = 10 @classmethod def setUpClass(cls): print('setUpClass') def setUp(self): print('setUp') def test_001(self): print('test_first') self.assertEqual(0, 0) @unittest.skipIf(i > 1, 'info') def test_002(self): print('test_second') self.assertEqual(0, 0) def test_003(self): print('test_third') self.assertEqual(0, 0) def test_004(self): print('test_fourth') self.assertEqual(0, 0) def tearDown(self): print('tearDown') @classmethod def tearDownClass(cls): print('tearDownClass')if __name__ == '__main__': unittest.main()

因为变量​​i = 1​​​,满足​​i > 0​​​的条件,所以跳过执行​​test_002​​用例,运行结果如下:

============================= test session starts =============================collecting ... collected 4 itemstestclass.py::MyTestClassOne::test_001 setUpClassPASSED [ 25%]setUptest_firsttearDowntestclass.py::MyTestClassOne::test_002 SKIPPED (info) [ 50%]Skipped: infotestclass.py::MyTestClassOne::test_003 PASSED [ 75%]setUptest_thirdtearDowntestclass.py::MyTestClassOne::test_004 PASSED [100%]setUptest_fourthtearDowntearDownClass======================== 3 passed, 1 skipped in 0.03s =========================Process finished with exit code 0

不满足条件跳过

下面的代码借助@unittest.skipUnless(a==5,‘info’)装饰器,演示当不满足某个条件时,跳过执行某个方法。

import unittestclass MyTestClassOne(unittest.TestCase): i = -1 @classmethod def setUpClass(cls): print('setUpClass') def setUp(self): print('setUp') self.i = 1 def test_001(self): print('test_first') self.assertEqual(0, 0) def test_002(self): print('test_second') self.assertEqual(0, 0) def test_003(self): print('test_third') self.assertEqual(0, 0) @unittest.skipUnless(i > 0, 'info') def test_004(self): print('test_fourth') self.assertEqual(0, 0) def tearDown(self): print('tearDown') @classmethod def tearDownClass(cls): print('tearDownClass')if __name__ == '__main__': unittest.main()

因为​​i < 0​​​不满足条件,所以跳过​​test_004​​用例,运行结果如下:

============================= test session starts =============================collecting ... collected 4 itemstestclass.py::MyTestClassOne::test_001 testclass.py::MyTestClassOne::test_002 testclass.py::MyTestClassOne::test_003 testclass.py::MyTestClassOne::test_004 ======================== 3 passed, 1 skipped in 0.03s =========================Process finished with exit code 0setUpClasssetUptest_firsttearDownsetUptest_secondtearDownsetUptest_thirdtearDownSKIPPED (info) Skipped: infotearDownClass


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:resultMap标签中里的collection标签详解
下一篇:Python3操作BeautifulSoup基础语法(python安装beautifulsoup4库)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~