Skip to content

allow to pass argvalues as an iterator to pytest.mark.parametrize() #122

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
pytestbot opened this issue Feb 16, 2012 · 10 comments
Closed

allow to pass argvalues as an iterator to pytest.mark.parametrize() #122

pytestbot opened this issue Feb 16, 2012 · 10 comments
Labels
topic: parametrize related to @pytest.mark.parametrize type: enhancement new feature or API change, should be merged into features branch

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt)



@pytestbot
Copy link
Contributor Author

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


i noticed a deeper issue,

just consuming the map object will break parametrizing anything that reuses it

@pytestbot
Copy link
Contributor Author

Original comment by Rafał Stożek (BitBucket: rafales, GitHub: rafales):


For anyone who will have the same issue: if you pass results of zip(), map(), range(), filter() etc. to a marker like parametrize make sure you wrap it in a list() call like this:

#!python

@pytest.mark.parametrize(('method',), list(zip(HTTP_METHODS)))
def test_foo(method):
    # code

because in python 3 those methods return a generator instead of a list.

@pytestbot
Copy link
Contributor Author

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


pytest-trunk already handles the case when you use map/zip/range/filter in parametrize but as @RonnyPfannschmidt points out, if you apply the same marker instance multiple times you'll get a "skip" on the second application because the iterator is exhausted. I guess parametrize() should explode the values early on to avoid this issue.

@pytestbot
Copy link
Contributor Author

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


clarify that pytest.mark.parametrize() takes a list of argvalues and not just
arbitrary iterators. Addresses issue122.

@pytestbot
Copy link
Contributor Author

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


While params in @pytest.fixture(..., params=...) can be iterators, the parametrize marker requires lists (as opposed to iterators) in the general case. Allowing to use iterators requires an "early" explosion of the iterator so that the marker can be applied multiple times. Particularly on Python3 that would allow to use "range" or "map" for constructing the argvalues without the need to wrap them in a list. The latter is a bit surprising for newcomers.

@pytestbot pytestbot added the type: enhancement new feature or API change, should be merged into features branch label Jun 15, 2015
@nicoddemus
Copy link
Member

@hpk42 and @RonnyPfannschmidt, this works for me in Python 3.4 with the latest master:

import pytest
pp = pytest.mark.parametrize('i', range(3))

@pp
def test_foo(i):
    pass

@pp
def test_bar(i):
    pass 

py.test -v outputs:

test_foo.py::test_foo[0] PASSED
test_foo.py::test_foo[1] PASSED
test_foo.py::test_foo[2] PASSED
test_foo.py::test_bar[0] PASSED
test_foo.py::test_bar[1] PASSED
test_foo.py::test_bar[2] PASSED

I couldn't find anything in the CHANGELOG mentioning this issue. Is this still an issue, or can we close this?

@hpk42
Copy link
Contributor

hpk42 commented Jul 14, 2015

it still fails with examples like this:

import pytest

p = pytest.mark.parametrize("x", iter([1,2,3]))

@p 
def test_qwe(testdir, x):
     pass

@p
def test_qwe2(testdir, x):
    pass

@RonnyPfannschmidt
Copy link
Member

@nicoddemus unlike general iterators, like for example map/imap objects, a range can be consumed multiple times

@nicoddemus
Copy link
Member

@RonnyPfannschmidt oh I didn't know that, thanks!

@pfctdayelise pfctdayelise added the topic: parametrize related to @pytest.mark.parametrize label Jul 25, 2015
@RonnyPfannschmidt
Copy link
Member

Closing this one, as its messy to support and a actual user error, perhaps there should be a warning for non-sequences?

mgorny pushed a commit to mgorny/pytest that referenced this issue May 27, 2023
----
ci: Test against Python 3.7

----
ci: Skip Coveralls submission if COVERALLS_REPO_TOKEN env. var is empty

----
ci: Update circleci image to allow install of python 3.7
See https://circleci.com/docs/2.0/configuration-reference/#machine

----
pytest-server-fixture: Fix rethinkdb tests requiring rethinkdb < 2.4.0

The current implementation needs to be refactored to support the latest
release of rethinkdb-python package. See pytest-dev#132

----
wheels: Generate universal wheels installable with both python 2.x and 3.x
See pytest-dev#125

Fix DeprecationWarning warnings using "logger.warning()" function

----
pytest-virtualenv: Add virtualenv as install requirement. Fixes pytest-dev#122

Remove requirement for pytest<4.0.0
See pytest-dev#100

----
pytest-webdriver: Fix RemovedInPytest4Warning using getfixturevalue

This commit fixes the following error:

  >           root_uri.append(request.getfuncargvalue('pyramid_server').uri)
  E           RemovedInPytest4Warning: getfuncargvalue is deprecated, use getfixturevalue

  pytest_webdriver.py:62: RemovedInPytest4Warning

See pytest-dev#100

----
pytest-verbose-parametrize: Add support for revamped marker infrastructure

This commit fixes tests failing with pytest > 4.0.0

----
pytest-verbose-parametrize: Fix integration tests to support pytest >= 4.1.0

This commit updates tests to support version of pytest with
pytest-dev/pytest@e9b2475e2 (Display actual test ids in `--collect-only`)

----
dist: Remove support for building and distributing *.egg files

Wheel is currently considered the standard for built and binary
packaging for Python

See https://packaging.python.org/discussions/wheel-vs-egg/

See pytest-dev#125

----
VagrantFile: Install python 3.7 and initialize python 3.7 by default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: parametrize related to @pytest.mark.parametrize type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

5 participants