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

Introduce "ids" argument to pytest.fixture #238

Closed
pytestbot opened this issue Dec 13, 2012 · 9 comments
Closed

Introduce "ids" argument to pytest.fixture #238

pytestbot opened this issue Dec 13, 2012 · 9 comments
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Gustavo Picon (BitBucket: tabo, GitHub: tabo)


One of the things I missed when I ported a hacky dynamic unittest suite to pytest fixtures, was the information I got from the test names. I parametrize 6 different django models in every test, but in the report I saw something like:

test_add_sibling_first[model0]
test_add_sibling_first[model1]
test_add_sibling_first[model2]
...

Which wasn't helpful at all. I solved this problem by monkeypatching _pytest.python.idmaker. Now I have nicer results, like:

test_add_sibling_first[AL_TestNode]
test_add_sibling_first[AL_TestNodeProxy]
test_add_sibling_first[MP_TestNode]
...

It would be better if pytest provided an interface for plugins to handle different object types, so for instance pytest_django could register itself as being in charge of formatting all django orm objects and views.


@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


Do you perform your parametrization through mark.parametrize or the pytest_generate_tests hook? In both cases you can pass in an "ids" list which effectively disables the id-maker.

@pytestbot
Copy link
Contributor Author

Original comment by Gustavo Picon (BitBucket: tabo, GitHub: tabo):


I set params in a fixture, like:

@pytest.fixture(scope='function', params=[model1, model2, model3])
def model(request):

And then used a model arg in my tests. I didn't want to use parametrize because I wanted cleaner tests, not decorator noise in each test case.

I suppose adding an ids arg to pytest.fixture would work.

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


Yes, makes sense. Can you open an issue for adding ids to pytest.fixture?

Scrap that, we have an issue, i am going to change the title :)

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


i wonder - can we maybe accept OrderedDicts as params?

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


i guess we could accept an Odict, but it's likely this dict is prepared somewhere else anyway and then splitting it into values and ids is not hard. What is more interesting is to allow specifying params=function which is called with the config object and can return values/ids. Currently you kind of have to revert to using pytest_generate_tests to achieve something similar.

@pytestbot
Copy link
Contributor Author

Original comment by Vinicius K. Ruoso (BitBucket: vkruoso, GitHub: vkruoso):


I was looking at the pytest.org website and this came up: http:https://pytest.org/latest/fixture.html#parametrizing-a-fixture

There is an example that uses the "ids" parameter on pytest.fixture:

#!python

def idfn(fixture_value):
    if fixture_value == 0:
        return "eggs"
    else:
        return None

@pytest.fixture(params=[0, 1], ids=idfn)
def b(request):
    return request.param

I was trying to use this in my tests, but an error occur. Looking in the history for python.py file and concluded that support for passing a callable to the ids parameter was added by issue351. By the changelog in the site I can see that this is listed in version 2.7.0.dev, and of course the version I have (2.6.4) lack support for it.

Why there is a feature documented on the main site that has not been released yet? This is confusing in my opinion.
Regardless of this issue, there is any timeline for 2.7.0 release?

Thanks a lot.

@pytestbot
Copy link
Contributor Author

Original comment by Brianna Laugher (BitBucket: pfctdayelise, GitHub: pfctdayelise):


Yes, this was added in a PR for #351 .

@vkruoso you are not the only one who has been confused by the docs being ahead of the latest release. Adding documentation at time of writing the feature means the website might (will probably) get updated before the next release. Perhaps for new functionality, it should be required to mention in the docs what version it will be available in. But this could be hard to know, eg when there is a version bump. So...........

Re release of 2.7, Holger mentioned on the pytest-dev list that he would like to make the release this week.

@pytestbot
Copy link
Contributor Author

Original comment by Vinicius K. Ruoso (BitBucket: vkruoso, GitHub: vkruoso):


Thanks for the fast answer. Maybe the documentation should be built from a stable branch or something similar. It is very frustrating to read about a feature and not be able to use it in a stable release.

Good to hear that 2.7 will be out soon. Will be waiting to use this feature.
With that in mind, is this issue already done? Or this is open because the "params=function" said before needs implementation?

@pytestbot
Copy link
Contributor Author

Original comment by Brianna Laugher (BitBucket: pfctdayelise, GitHub: pfctdayelise):


Actually, the support for supplying ids as a list of strings to pytest.fixture was added in 2013-12 by @flub, although the docs showing the example with ids as a fn was added in 2014-10 (after the PR for issue 351). So pytest.fixture is using the same mechanism as pytest.mark.parametrize to create the test ids, although I'll admit I couldn't figure out precisely how. So the examples in the docs will work as specified in 2.7.0.

I think Holger's comment about "allow specifying params=function" is more of a musing than a requirement for this issue, as stated in the title this issue is resolved. Specifying fixture params as a function could be reported a separate issue if it is not already, if anyone cares enough about it to do so.

@pytestbot pytestbot added the type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature label Jun 15, 2015
fkohlgrueber pushed a commit to fkohlgrueber/pytest that referenced this issue Oct 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

1 participant