微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 如何自定义pytest名称

我想自定义我的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_@R_558_4045@ion()
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私有变量.

创建一个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打印

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐