TvoroG / pytest-lazy-fixture

It helps to use fixtures in pytest.mark.parametrize
MIT License
379 stars 30 forks source link

Use fixture names to generate test ids? #46

Closed avirshup closed 4 years ago

avirshup commented 4 years ago

First off, thank you for writing this plugin, it fixes problems that have been bugging me for years.

Given the following tests:

import pytest
from pytest_lazyfixture import lazy_fixture

@pytest.fixture()
def foo():
    return "foo"

@pytest.fixture(params=['spam', 'eggs'])
def bar(request):
    return f"bar-{request.param}"

@pytest.mark.parametrize("data", [lazy_fixture("foo"),
                                  lazy_fixture("bar")])
def test_the_thing(data):
    assert False    

Pytest generates the following test names:

> py.test test_lazy.py --collect-only
===================== test session starts =====================
platform darwin -- Python 3.7.6, pytest-5.3.2, py-1.8.1, pluggy-0.13.1
rootdir: /tmp
plugins: xdist-1.26.1, flask-0.14.0, lazy-fixture-0.6.2, forked-1.1.3
collected 3 items                                                                                                            
<Module test_lazy.py>
  <Function test_the_thing[data0]>
  <Function test_the_thing[data1-spam]>
  <Function test_the_thing[data1-eggs]>

Would it be possible to use the fixture name to generate these ids? It would be great to end up with these names instead:


test_the_thing[foo]
test_the_thing[bar-spam]
test_the_thing[bar-eggs]
YannickJadoul commented 4 years ago

As a workaround, these two should work, though:

@pytest.mark.parametrize("data",
                         [lazy_fixture("foo"), lazy_fixture("bar")],
                         ids=["foo", "bar"])
@pytest.mark.parametrize("data",
                         [pytest.fixture(lazy_fixture("foo"), id="foo"),
                          pytest.fixture(lazy_fixture("bar"), id="bar")])
YannickJadoul commented 4 years ago

Actually, using pytest_make_parametrize_id, this was easier to implement than expected. PR ready, see #47 :-)

avirshup commented 4 years ago

That's fantastic - just checked out the branch and it works perfectly. Always nice to see a new feature that only needs 3 lines of code :) Thanks so much!

TvoroG commented 4 years ago

Finally shipped 0.6.3 version with this feature :)