代码之家  ›  专栏  ›  技术社区  ›  George Mauer

如何自定义pytest名称

  •  4
  • George Mauer  · 技术社区  · 7 年前

    我想自定义pytest的输出名称,以包括我的fixture的名称

    所以我有

    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'
    

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

    1 回复  |  直到 7 年前
        1
  •  3
  •   Gelineau    7 年前

    conftest.py

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

    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打印。