Skip to content

Why cant decorator parametrize automatically get function's arguments? #1742

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

Closed
nitrocode opened this issue Jul 19, 2016 · 5 comments
Closed
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@nitrocode
Copy link

nitrocode commented Jul 19, 2016

I don't understand why a pytest's test function's params have to be manually kept consistent using the pytest.mark.parametrize function.

Isn't there a way for pytest to automatically grab the arguments ("fileName", "fields", "thresh", "companies") and assume that the fields provided (fileNameA, fileNameAFields, 5, False) are in the correct order to feed it into test_me() ?

    @pytest.mark.parametrize(
        ("fileName", "fields", "thresh", "companies"), [ # this line should be automatic
            (fileNameA, fileNameAFields, 5, False),
            (fileNameB, fileNameBFields, 5, True),
        ],
        ids=[
            'FileA',
            'FileB'
        ]
    )
    def test_me(self, fileName, fields, thresh, companies):
    ...
@The-Compiler
Copy link
Member

You can also mix fixtures and parametrization - sure, pytest could just assume everything which isn't a fixture is parametrized, but that sounds error-prone.

@RonnyPfannschmidt RonnyPfannschmidt added the type: question general question, might be closed after 2 weeks of inactivity label Jul 19, 2016
@RonnyPfannschmidt
Copy link
Member

the parameterize mark is independent of the test function, it could also be used in a global mark, or in a class level mark

@nicoddemus
Copy link
Member

A quick example of the latter, from pytest's own test suite:

@pytest.mark.parametrize('flavor', ['fixture', 'yield_fixture'])
class TestContextManagerFixtureFuncs:

    def test_simple(self, testdir, flavor):
        ...

    def test_scoped(self, testdir, flavor):
        ...

@mgeier
Copy link

mgeier commented Jun 24, 2017

@nitrocode I know I'm quite late with this, but probably you (or somebody else who stumbles upon this) might still find this useful.

I totally agree that the argument names should be deduced automatically. I asked for this in #518 and I suggested an implementation in #780.

Sadly, this wasn't accepted, but I got some help for implementing this as an extension:

https://github.com/mgeier/pytest-auto-parametrize/
https://pypi.python.org/pypi/pytest-auto-parametrize/

I still hope that it (or something similar) gets included in core pytest at some point, because as a plugin it has around zero visibility, while if it were mentioned in the docs, people would actually know about this feature ...

@RonnyPfannschmidt
Copy link
Member

@mgeier i propose adding a section about it in the docs then as pr

while its properties cant correctly work in various interactions with parameterization from different souses its still helpful for people

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

5 participants