gpt4 book ai didi

python - 如何自定义pytest名称

转载 作者:太空狗 更新时间:2023-10-30 02:53:09 26 4
gpt4 key购买 nike

我想自定义我的 pytest 的输出名称以包含我的固定装置的名称

所以我有

def test_t1(
when_creating_a_project_from_a_sales_handoff,
with_a_new_customer,
and_no_conflicting_data_exists,
create_project):
it_will_create_a_customer_with_the_releavant_information()
it_will_create_a_project_that_references_the_newly_created_customer()

我希望显示的测试名称是某个版本的

when_creating_a_project_from_a_sales_handoff
with_a_new_customer
and_no_conflicting_data_exists
create_project

我该怎么做?我尝试创建

@fixture
def namer(request):
request.node.name = 'test_foo'

但没有骰子,它没有改变测试显示名称

最佳答案

我设法更改了显示的名称,但只能使用 pytest 私有(private)变量。

制作一个conftest.py文件并制作这个函数:

def pytest_itemcollected(item):
""" change test name, using fixture names """
item._nodeid = ', '.join(item._fixtureinfo.argnames)

当我用 pytest 运行这个 test_file 时:

import pytest

@pytest.fixture()
def fixture_1():
pass

@pytest.fixture()
def fixture_2():
pass

def test1(fixture_1):
assert 1 == 1

def test_a(fixture_1, fixture_2):
assert 1 == 2

结果是:

pytest
============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.6.1, py-1.5.3, pluggy-0.6.0
rootdir: C:\Users\gelineau\Desktop\kaggle_flavien, inifile:
collected 2 items

fixture_1 . [ 50%]
fixture_1, fixture_2 F [100%]

================================== FAILURES ===================================
___________________________________ test_a ____________________________________

fixture_1 = None, fixture_2 = None

def test_a(fixture_1, fixture_2):
> assert 1 == 2
E assert 1 == 2

test\test_so.py:15: AssertionError
===================== 1 failed, 1 passed in 0.86 seconds ======================

新的测试名称也打印在pycharm中

关于python - 如何自定义pytest名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50767234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com