本文讲述以下几个小知识点:
环境准备:
项目目录:
smoke_testing_demo
test_case
__init__.py
test_case_with_unittest.py
test_case_with_pytest.py
run_unittest_smoke_testing.py
一、Unittest如何组织冒烟用例
loadTestsFromNames 方法简介
# loader.py 该文件在python3.7已不存在,建议使用python3.64 查看使用方法
class TestLoader(object):
"""
该类负责根据各种标准加载测试并将它们包装在TestSuite中
"""
def loadTestsFromNames(self, names, module=None):
"""
返回给定的一组用例名的测试用例的套件
"""
loadTestsFromNames 组织冒烟用例
# 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
小结:
二、Pytest 组织冒烟测试
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 ===========================
三、Pytest 执行 Unittest冒烟用例
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 ==============
总结:
pytest -m key test_case.py
即可。