之前有一段时间一直用 Python Uittest做自动化测试,觉得Uittest组织冒烟用例比较繁琐,后来我一测试开发大佬 :【知一】提示我使用pytest.mark来组织冒烟用例
本文讲述以下几个内容:
1、Unittest 如何组织冒烟用例
2、Pytest 组织冒烟测试
3、Pytest 执行unittest冒烟用例
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
$ loader.py 该文件在python3.7已不存在,建议使用python3.64 查看使用方法
class TestLoader(object):
"""
该类负责根据各种标准加载测试并将它们包装在TestSuite中
"""
def loadTestsFromNames(self, names, module=None):
"""
返回给定的一组用例名的测试用例的套件
"""
测试用例
$ 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)
冒烟测试用例集
$ 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
测试用例
$ 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 ===========================
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 ==============
1、Uittest组织冒烟用例,需通过loadTestsFromNames在不同的测试模块里指定测试用例,组装成test suit(测试套件)后,给TextTestRunner运行
2、Pytest组织冒烟用例,只需给测试用例加上@pytest.mark.key ,使用命令行pytest -m key test_case.py 即可
个人见解: