$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 – Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected
test_demo.py . [100%]
=================================================================== 1 passed, 2 deselected in 0.04s ====================================================================
方式二:在conftest.py文件中重写pytest_configure函数即可,比如如下,注册两个mark:smoke和test
def pytest_configure(config):
config.addinivalue_line(
“markers”, “smoke: smoke test”
)
config.addinivalue_line(
“markers”, “test: system test”
)
test_demo.py代码如下:
import pytest
@pytest.mark.smoke
def test_func():
assert 1==1
@pytest.mark.test
def test_func2():
assert 1==1
def test_func3():
assert 1==1
通过pytest -m test 执行结果如下:
$ pytest -m test
========================================================================= test session starts ==========================================================================
platform win32 – Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected
test_demo.py . [100%]
=================================================================== 1 passed, 2 deselected in 0.02s ====================================================================
四、对未注册mark的限制
默认情况下,对未注册mark直接使用是会产生一条告警信息,比如这里把pytest.ini和conftest.py都删除掉,只剩test_demo.py一个文件
test_demo.py代码如下
import pytest
@pytest.mark.smoke
def test_func():
assert 1==1
@pytest.mark.test
def test_func2():
assert 1==1
def test_func3():
assert 1==1
直接使用 pytest -m smoke 执行结果如下,可以发现这里产生了两条告警,这就是因为这两条告警未在pytest.ini或者conftest.py中进行注册的原因,在实际项目开发中如果在执行测试的时候发现了这种大片告警打印,解决办法就是在pytest.ini或者conftest.py将这些告警报出的mark都进行注册即可
$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 – Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected
test_demo.py . [100%]
=========================================================================== warnings summary ===========================================================================
test_demo.py:3
D:\src\blog\tests\test_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo? You can register custom marks to avoid this warning - for deta
ils, see https://docs.pytest.org/en/stable/mark.html
@pytest.mark.smoke
test_demo.py:7
D:\src\blog\tests\test_demo.py:7: PytestUnknownMarkWarning: Unknown pytest.mark.test - is this a typo? You can register custom marks to avoid this warning - for detai
ls, see https://docs.pytest.org/en/stable/mark.html
@pytest.mark.test
– Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================= 1 passed, 2 deselected, 2 warnings in 0.02s ==============================================================
如果希望强制限制必须先注册再使用mark,则可以在pytest.ini中加上如下配置即可
[pytest]
addopts = --strict-markers
比如test_demo.py代码:
import pytest
@pytest.mark.smoke
def test_func():
assert 1==1
@pytest.mark.test
def test_func2():
assert 1==1
def test_func3():
assert 1==1
此时继续使用pytest -m smoke执行结果如下,发现此时已经报错了,即强制限制必须对mark进行注册
$ pytest -m smoke
========================================================================= test session starts ==========================================================================
platform win32 – Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 0 items / 1 error
================================================================================ ERRORS ================================================================================
____________________________________________________________________ ERROR collecting test_demo.py _____________________________________________________________________
‘smoke’ not found in markers
configuration option
======================================================================= short test summary info ========================================================================
ERROR test_demo.py
!!! Interrupted: 1 error during collection !!!
=========================================================================== 1 error in 0.14s ===========================================================================
五、 参数化固定装置、测试函数
Pytest可以在以下几个级别上实现测试参数化:
pytest.fixture()
允许 parametrize fixture functions
@pytest.mark.parametrize
定义多组参数、固定装置在测试函数、类上面
pytest_generate_tests
允许用户定义自定义参数化方案或扩展。
1.参数化测试函数:@pytest.mark.parametrize
(1) @parametrize 装饰器定义了三个不同的元组
@parametrize
装饰器定义了三个不同的(test_input,expected)
元组,以便test_eval
函数将依次使用它们运行三次:
content of test_expectation.py
import pytest
@pytest.mark.parametrize(“test_input,expected”, [(“3+5”, 8), (“2+4”, 6), (“6*9”, 42)])
def test_eval(test_input, expected):
assert eval(test_input) == expected
C:\Users\Desktop\python>pytest test_expectation.py
================================================== test session starts ===================================================
platform win32 – Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: C:\Users\X21201\Desktop\python
collected 3 items
test_expectation.py …F [100%]
======================================================== FAILURES ========================================================
___________________________________________________ test_eval[69-42] ____________________________________________________
test_input = '69’, expected = 42
@pytest.mark.parametrize(“test_input,expected”, [(“3+5”, 8), (“2+4”, 6), (“6*9”, 42)])
def test_eval(test_input, expected):
assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('69’)
test_expectation.py:7: AssertionError
================================================ short test summary info =================================================
FAILED test_expectation.py::test_eval[69-42] - AssertionError: assert 54 == 42
============================================== 1 failed, 2 passed in 0.07s ===============================================
(2)在类或模块上使用参数化标记
import pytest
@pytest.mark.parametrize(“n,expected”, [(1, 2), (3, 4)])
class TestClass:
def test_simple_case(self, n, expected):
assert n + 1 == expected
def test_weird_simple_case(self, n, expected):
assert (n * 1) + 1 == expected
复制代码
collected 4 items
test_expectation.py::TestClass::test_simple_case[1-2] PASSED
test_expectation.py::TestClass::test_simple_case[3-4] PASSED
test_expectation.py::TestClass::test_weird_simple_case[1-2] PASSED
test_expectation.py::TestClass::test_weird_simple_case[3-4] PASSED
(3)参数化一个py文件中所有测试
import pytest
pytestmark = pytest.mark.parametrize(“n,expected”, [(1, 2), (3, 4)])
class TestClass:
def test_simple_case(self, n, expected):
assert n + 1 == expected
def test_weird_simple_case(self, n, expected):
assert (n * 1) + 1 == expected
class TestDemo:
def test_simple_case_001(self, n, expected):
assert n + 1 == expected
collected 6 items
test_expectation.py::TestClass::test_simple_case[1-2] PASSED
test_expectation.py::TestClass::test_simple_case[3-4] PASSED
test_expectation.py::TestClass::test_weird_simple_case[1-2] PASSED
test_expectation.py::TestClass::test_weird_simple_case[3-4] PASSED
test_expectation.py::TestDemo::test_simple_case_001[1-2] PASSED
test_expectation.py::TestDemo::test_simple_case_001[3-4] PASSED
(4)参数化中标记单个测试实例:使用 mark.xfail:
import pytest
@pytest.mark.parametrize(“test_input,expected”,[(“3+5”, 8), (“2+4”, 6), pytest.param(“6*9”, 42, marks=pytest.mark.xfail)],)
def test_eval(test_input, expected):
assert eval(test_input) == expected
collected 3 items
test_expectation.py::test_eval[3+5-8] PASSED
test_expectation.py::test_eval[2+4-6] PASSED
test_expectation.py::test_eval[6*9-42] XFAIL
(5) 参数的所有组合:堆叠参数化装饰器
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数软件测试工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上软件测试开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注软件测试)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上软件测试开发知识点,真正体系化!**
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-MUydDTA6-1713074638821)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!