测试函数标记
pytest提供了标记机制,允许你使用marker对测试函数(测试用例)做标记,一个测试函数(测试用例)可以有多个marker,一个marker也可以用例标记多个测试函数(测试用例)。针对冒烟测试,我们可以使用marker标记,检查系统有没有重大缺陷,因为冒烟测试通常是不会包含全套测试的。
选定了两个测试函数(测试用例)来作为冒烟测试,首先我们在 test_run_pass() 、test_run_fail() 两个测试函数(测试用例)的头部添加装饰器 @pytest.mark.smoking; @pytest.mark.get_testing(注意:smoking;get_testing 标记并非pytest 内置的,是我自己在配置文件定义的)。
import pytest
@pytest.mark.smoking
def test_run_pass():
expected = (1, 2, 3)
assert expected == (1, 2, 3)
@pytest.mark.smoking
def test_run_fail():
expected = (1, 2, 3)
assert expected == (1, 2, 4)
运行展示结果如下:
osts\ReadPytest>pytest -v -m 'smoking' test_two.py
================================================================================== test session starts ===================================================================================
collected 2 items
test_two.py::test_run_pass PASSED [ 50%]
test_two.py::test_run_fail FAILED [100%]
======================================================================================== FAILURES ========================================================================================
_____________________________________________________________________________________ test_run_fail ______________________________________________________________________________________
@pytest.mark.smoking
def test_run_fail():
expected = (1, 2, 3)
> assert expected == (1, 2, 4)
E assert (1, 2, 3) == (1, 2, 4)
E At index 2 diff: 3 != 4
E Full diff:
E - (1,