Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fixture names to generate test ids? #46

Closed
avirshup opened this issue Jan 16, 2020 · 4 comments
Closed

Use fixture names to generate test ids? #46

avirshup opened this issue Jan 16, 2020 · 4 comments

Comments

@avirshup
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

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

@avirshup
Copy link
Author

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
Copy link
Owner

TvoroG commented Feb 1, 2020

Finally shipped 0.6.3 version with this feature :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants