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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameterize() as an alias for parametrize() #3159

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 43 additions & 1 deletion _pytest/python.py
Expand Up @@ -112,7 +112,7 @@ def pytest_cmdline_main(config):
def pytest_generate_tests(metafunc):
# those alternative spellings are common - raise a specific error to alert
# the user
alt_spellings = ['parameterize', 'parametrise', 'parameterise']
alt_spellings = ['parametrise', 'parameterise']
for attr in alt_spellings:
if hasattr(metafunc.function, attr):
msg = "{0} has '{1}', spelling should be 'parametrize'"
Expand Down Expand Up @@ -743,6 +743,48 @@ def __init__(self, function, fixtureinfo, config, cls=None, module=None):
self._ids = set()
self._arg2fixturedefs = fixtureinfo.name2fixturedefs

def parameterize(self, argnames, argvalues, indirect=False, ids=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are to include this, I prefer if you would just create an actual alias in the class:

parameterize = parametrize

While mentioning in the docs that parameterize is a valid alias; we don't want to have the documentation duplicated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does seem like a more elegant solution. Should it be placed below the parametrize definition with a comment stating it's an alias?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

Please add a test which uses the new spelling as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble getting the test to work, so far I have this:

def test_parameterize(self, testdir):
    testdir.makepyfile("""
        import pytest
        @pytest.mark.parameterize("foo", [1, 2, 3])
        def test_foo(foo):
            assert foo in [1, 2, 3]
    """)
    reprec = testdir.inline_run()
    reprec.assertoutcome(passed=3)

And it complains about fixture 'value' not found.
If I use parametrize instead, it does work.
What am I doing wrong? 馃

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value? Perhaps you mean foo? If that's the case I think you also need to handle that name here:

parametrize_func = getattr(metafunc.function, 'parametrize', None)

Perhaps something like: getattr(metafunc.function, 'parametrize', None) or getattr(metafunc.function, 'parameterize', None) (ugh).

Otherwise please post the full error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I meant foo. 馃槄 (I was also testing on a separate file where I used value instead)

I changed it but still got the same error, posting the full error below:

Error
==================================== ERRORS ====================================
__________________________ ERROR at setup of test_foo __________________________
file /tmp/pytest-of-alanbato/pytest-30/test_parameterize0/test_parameterize.py, line 2
  @pytest.mark.parameterize("foo", [1, 2, 3])
  def test_foo(foo):
E       fixture 'foo' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/tmp/pytest-of-alanbato/pytest-30/test_parameterize0/test_parameterize.py:2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You applied the change I mentioned in fixtures.py right? Hmm so it must be missing somewhere else... not sure where, a quick search didn't turn up anything obvious.

But I suggest for you to not spend too much time on this until we decide for sure if we want to merge this or not. 馃槄

scope=None):
"""An alias of ``parametrize``.
Add new invocations to the underlying test function using the list
of argvalues for the given argnames. Parametrization is performed
during the collection phase. If you need to setup expensive resources
see about setting indirect to do it rather at test setup time.

:arg argnames: a comma-separated string denoting one or more argument
names, or a list/tuple of argument strings.

:arg argvalues: The list of argvalues determines how often a
test is invoked with different argument values. If only one
argname was specified argvalues is a list of values. If N
argnames were specified, argvalues must be a list of N-tuples,
where each tuple-element specifies a value for its respective
argname.

:arg indirect: The list of argnames or boolean. A list of arguments'
names (subset of argnames). If True the list contains all names from
the argnames. Each argvalue corresponding to an argname in this list will
be passed as request.param to its respective argname fixture
function so that it can perform more expensive setups during the
setup phase of a test rather than at collection time.

:arg ids: list of string ids, or a callable.
If strings, each is corresponding to the argvalues so that they are
part of the test id. If None is given as id of specific test, the
automatically generated id for that argument will be used.
If callable, it should take one argument (a single argvalue) and return
a string or return None. If None, the automatically generated id for that
argument will be used.
If no ids are provided they will be generated automatically from
the argvalues.

:arg scope: if specified it denotes the scope of the parameters.
The scope is used for grouping tests by parameter instances.
It will also override any fixture-function defined scope, allowing
to set a dynamic scope using test context or configuration.
"""
self.parametrize(self, argnames, argvalues, indirect, ids, scope)

def parametrize(self, argnames, argvalues, indirect=False, ids=None,
scope=None):
""" Add new invocations to the underlying test function using the list
Expand Down
1 change: 1 addition & 0 deletions changelog/3159.feature
@@ -0,0 +1 @@
Add ``parameterize()`` function call as an alias for ``parametrize()``