前言:以前有一段时间一直用 Python Uittest作自动化测试,以为Uittest组织冒烟用例比较繁琐,后来康哥提示我使用pytest.mark来组织冒烟用例html
本文讲述如下几个内容:python
一、Unittest 如何组织冒烟用例
二、Pytest 组织冒烟测试
三、Pytest 执行unittest冒烟用例git
环境准备:github
Python 3.64
Pytest 5.01数组
项目目录:框架
smoke_testing_demo test_case __init__.py test_case_with_unittest.py test_case_with_pytest.py run_unittest_smoke_testing.py
<br>测试
$ loader.py 该文件在python3.7已不存在,建议使用python3.64 查看使用方法 class TestLoader(object): """ 该类负责根据各类标准加载测试并将它们包装在TestSuite中 """ def loadTestsFromNames(self, names, module=None): """ 返回给定的一组用例名的测试用例的套件 """
测试用例ui
$ test_case_with_unittest.py #!/usr/bin/env python3 # encoding:utf-8 import unittest class TestUittestCase(unittest.TestCase): def test_case_with_unittest_1(self): '''冒烟测试用例''' print('I am Smoke Testing ') def test_case_with_unittest_2(self): pass if __name__ == '__main__': unittest.main(verbosity=2)
$ test_case_with_unittest2.py #!/usr/bin/env python3 # encoding:utf-8 import unittest class TestUittestCase2(unittest.TestCase): def test_case_with_unittest_3(self): '''冒烟测试用例''' print('I am Smoke Testing ') def test_case_with_unittest_4(self): pass if __name__ == '__main__': unittest.main(verbosity=2)
冒烟测试用例集spa
$ run_unittest_smoke_testing.py #!/usr/bin/env python3 # encoding:utf-8 import unittest cases = [ 'test_case.test_case_with_unittest2.TestUittestCase2.test_case_with_unittest_3', 'test_case.test_case_with_unittest.TestUittestCase.test_case_with_unittest_1' ] test_suit = unittest.TestLoader().loadTestsFromNames(cases) runner = unittest.TextTestRunner(verbosity=2) runner.run(test_suit)
运行结果命令行
test_case_with_unittest_3 (test_case.test_case_with_unittest2.TestUittestCase2) 冒烟测试 ... ok test_case_with_unittest_1 (test_case.test_case_with_unittest.TestUittestCase) 冒烟测试 ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.000s
<br>
ps: 更多经过loadTestsFromNames 使用技巧, 能够查看《Unittest组织用例的姿式》这篇博文,连接在文末
<br>
ps:更多的pytest.mark用法能够参考乙醇老师《安利一下pytest的mark用法》
测试用例
$ run_unittest_smoke_testing.py #!/usr/bin/env python3 # encoding:utf-8 import pytest @pytest.mark.test_env def test_case_1(): pass @pytest.mark.test_env @pytest.mark.smoke def test_case_2(): ''' 冒烟用例''' pass
cd 进入 /test_case目录, 使用命令行运行 test_case_with_pytest.py
pytest test_case_with_pytest.py -v -m smoke
运行结果
collected 2 items test_case_with_pytest.py::test_case_2 PASSED ============================== 1 tests deselected ============================== ==================== 1 passed, 1 deselected in 0.01 seconds ====================
运行被标记test_env的用例
pytest test_case_with_pytest.py -v -m test_env
运行结果
collected 2 items test_case_with_pytest.py::test_case_1 PASSED test_case_with_pytest.py::test_case_2 PASSED =========================== 2 passed in 0.01 seconds ===========================
<br>
Pytest测试框架是兼容Python自带的Unittest
修改test_case_with_unittest2.py
$ test_case_with_unittest2.py #!/usr/bin/env python3 # encoding:utf-8 import unittest import pytest class TestUittestCase2(unittest.TestCase): @pytest.mark.smoke def test_case_with_unittest_3(self): '''冒烟测试用例''' print('I am Smoke Testing ') def test_case_with_unittest_4(self): pass if __name__ == '__main__': unittest.main(verbosity=2)
命令行运行 test_case_with_unittest2.py
pytest test_case_with_unittest2.py -v -m smoke
运行结果
collected 2 items / 1 deselected / 1 selected test_case_with_unittest2.py::TestUittestCase2::test_case_with_unittest_3 PASSED [100%] ============== 1 passed, 1 deselected, 1 warnings in 0.01 seconds ==============
总结:
一、Uittest组织冒烟用例,需经过loadTestsFromNames在不一样的测试模块里指定测试用例,组装成test suit(测试套件)后,给TextTestRunner运行
二、Pytest组织冒烟用例,只需给测试用例加上@pytest.mark.key ,使用命令行pytest -m key test_case.py 便可
<br>
笔者我的看法:
使用Uittest组织冒烟测试,关注点有至少有两个
一、当编写新功能的冒烟测试,须要去维护冒烟测试用例集
二、合并代码时,若是有两我的同时修改了这个冒烟用例集,还要解决冲突,防止遗漏冒烟用例
使用Pytest组织冒烟测试,关注点在于用例的自己
当编写新功能的冒烟测试,我只需在给用例加一个编写用例人员约定好的@pytest.mark,例如@pytest.mark.smoke
《Python Unittest - 根据不一样测试环境跳过用例详解》
https://github.com/SEtester/smoke_testing_demo
<br>
__最后,欢迎同窗们留言, 你认为自动化冒烟测试 Unittest , Pytest 哪家强? __
文章若有不是,欢迎同窗们斧正 <br>
<br>