Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the pytest name

Tags:

python

pytest

I would like to customize the output name of my pytest to include the name of my fixtures

So I have

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()

and I'd like the displayed test name to be some version of

when_creating_a_project_from_a_sales_handoff
with_a_new_customer
and_no_conflicting_data_exists
create_project

How can I do this? I tried creating

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

but no dice, it didn't change the test display name

like image 804
George Mauer Avatar asked Jun 08 '18 19:06

George Mauer


People also ask

How do I structure a pytest file?

Project StructureThe modules containing pytests should be named “test_*. py” or “*_test.py”. While the pytest discovery mechanism can find tests anywhere, pytests must be placed into separate directories from the product code packages. These directories may either be under the project root or under the Python package.

How do you define pytest?

PyTest is a testing framework that allows users to write test codes using Python programming language. It helps you to write simple and scalable test cases for databases, APIs, or UI. PyTest is mainly used for writing tests for APIs. It helps to write tests from simple unit tests to complex functional tests.

What is pytest fixture decorator?

pytest. fixture decorator makes it possible to inject the return value in the test functions whose have in their signature the decorated function name. It's really more hard to figure out than just seeing it in action: def get_gender_heading(name): # super AI based algorithm (such 2018, much AI)


1 Answers

I managed to change the displayed name, but only by using pytest private variables.

Make a conftest.pyfile and make this function:

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

When I run this test_file with pytest:

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

The result is:

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 ======================

The new test names are also printed in pycharm

like image 88
Gelineau Avatar answered Sep 28 '22 00:09

Gelineau