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

Unable to pass keyword arguments into pytest.param marks #4395

Closed
AetherUnbound opened this issue Nov 14, 2018 · 2 comments
Closed

Unable to pass keyword arguments into pytest.param marks #4395

AetherUnbound opened this issue Nov 14, 2018 · 2 comments

Comments

@AetherUnbound
Copy link

Hello! Let me open by saying thanks all for the work you do on this package - I didn't like writing unit tests until I found pytest, and now I proselytize for it every chance I get 😀

It seems that it is now impossible (with pytest==4) to pass keyword arguments into parametrize parameters. Here's a small example

import pytest


@pytest.mark.parametrize('foo', [
    'a',
    'b',
    pytest.mark.xfail(1, reason='reason1'),
    pytest.mark.skip(None, reason='reason2')
])
def test_foo(foo):
    assert isinstance(foo, str)

In pytest<4 this was fine (although gave a deprecation warning):

 mattb  (e) pytest  ~  misc  pytest-mark  2  pip install 'pytest<4'
Looking in indexes: https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/simple, https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-snapshot-local/simple
Collecting pytest<4
  Downloading https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/packages/57/94/305477fb977546970a3464c21b63c6800df6705384af2978b89acccfb151/pytest-3.10.1-py2.py3-none-any.whl (216kB)
    100% |████████████████████████████████| 225kB 26.5MB/s 
Requirement already satisfied: more-itertools>=4.0.0 in /home/mattb/programs/anaconda3/envs/pytest/lib/python3.7/site-packages (from pytest<4) (4.3.0)
Requirement already satisfied: six>=1.10.0 in /home/mattb/programs/anaconda3/envs/pytest/lib/python3.7/site-packages (from pytest<4) (1.11.0)
Requirement already satisfied: py>=1.5.0 in /home/mattb/programs/anaconda3/envs/pytest/lib/python3.7/site-packages (from pytest<4) (1.7.0)
Requirement already satisfied: setuptools in /home/mattb/programs/anaconda3/envs/pytest/lib/python3.7/site-packages (from pytest<4) (40.5.0)
Requirement already satisfied: attrs>=17.4.0 in /home/mattb/programs/anaconda3/envs/pytest/lib/python3.7/site-packages (from pytest<4) (18.2.0)
Requirement already satisfied: atomicwrites>=1.0 in /home/mattb/programs/anaconda3/envs/pytest/lib/python3.7/site-packages (from pytest<4) (1.2.1)
Requirement already satisfied: pluggy>=0.7 in /home/mattb/programs/anaconda3/envs/pytest/lib/python3.7/site-packages (from pytest<4) (0.8.0)
Installing collected packages: pytest
  Found existing installation: pytest 4.0.0
    Uninstalling pytest-4.0.0:
      Successfully uninstalled pytest-4.0.0
Successfully installed pytest-3.10.1
 mattb  (e) pytest  ~  misc  pytest-mark  pytest .
============================================================================================================= test session starts =============================================================================================================
platform linux -- Python 3.7.1, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /home/mattb/programs/anaconda3/envs/pytest/bin/python
cachedir: .pytest_cache
rootdir: /home/mattb/misc/pytest-mark, inifile:
collected 4 items                                                                                                                                                                                                                             

test_foo.py::test_foo[a] PASSED                                                                                                                                                                                                         [ 25%]
test_foo.py::test_foo[b] PASSED                                                                                                                                                                                                         [ 50%]
test_foo.py::test_foo[1] xfail                                                                                                                                                                                                          [ 75%]
test_foo.py::test_foo[None] SKIPPED                                                                                                                                                                                                     [100%]

============================================================================================================== warnings summary ===============================================================================================================
test_foo.py:5
  test_foo.py:5: RemovedInPytest4Warning: Applying marks directly to parameters is deprecated, please use pytest.param(..., marks=...) instead.
  For more details, see: https://docs.pytest.org/en/latest/parametrize.html
    @pytest.mark.parametrize('foo', [
  test_foo.py:5: RemovedInPytest4Warning: Applying marks directly to parameters is deprecated, please use pytest.param(..., marks=...) instead.
  For more details, see: https://docs.pytest.org/en/latest/parametrize.html
    @pytest.mark.parametrize('foo', [

-- Docs: https://docs.pytest.org/en/latest/warnings.html
========================================================================================= 2 passed, 1 skipped, 1 xfailed, 2 warnings in 0.03 seconds ==========================================================================================

This is now no longer possible in pytest>=4:

 mattb  (e) pytest  ~  misc  pytest-mark  pip install pytest
Looking in indexes: https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/simple, https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-snapshot-local/simple
Collecting pytest
  Downloading https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/packages/bb/d5/7601c468ded9a59478dcb39d21e24d58bb375681c64a06fbb629d2bc2ac3/pytest-4.0.0-py2.py3-none-any.whl (217kB)
    100% |████████████████████████████████| 225kB 25.4MB/s 
Collecting six>=1.10.0 (from pytest)
  Downloading https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting atomicwrites>=1.0 (from pytest)
  Downloading https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/packages/3a/9a/9d878f8d885706e2530402de6417141129a943802c084238914fa6798d97/atomicwrites-1.2.1-py2.py3-none-any.whl
Collecting more-itertools>=4.0.0 (from pytest)
  Downloading https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/packages/79/b1/eace304ef66bd7d3d8b2f78cc374b73ca03bc53664d78151e9df3b3996cc/more_itertools-4.3.0-py3-none-any.whl (48kB)
    100% |████████████████████████████████| 51kB 34.1MB/s 
Collecting py>=1.5.0 (from pytest)
  Downloading https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/packages/3e/c7/3da685ef117d42ac8d71af525208759742dd235f8094221fdaafcd3dba8f/py-1.7.0-py2.py3-none-any.whl (83kB)
    100% |████████████████████████████████| 92kB 46.2MB/s 
Collecting attrs>=17.4.0 (from pytest)
  Downloading https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/packages/3a/e1/5f9023cc983f1a628a8c2fd051ad19e76ff7b142a0faf329336f9a62a514/attrs-18.2.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in /home/mattb/programs/anaconda3/envs/pytest/lib/python3.7/site-packages (from pytest) (40.5.0)
Collecting pluggy>=0.7 (from pytest)
  Downloading https://artifactory.corp.alleninstitute.org/artifactory/api/pypi/pypi-virtual/packages/1c/e7/017c262070af41fe251401cb0d0e1b7c38f656da634cd0c15604f1f30864/pluggy-0.8.0-py2.py3-none-any.whl
Installing collected packages: six, atomicwrites, more-itertools, py, attrs, pluggy, pytest
Successfully installed atomicwrites-1.2.1 attrs-18.2.0 more-itertools-4.3.0 pluggy-0.8.0 py-1.7.0 pytest-4.0.0 six-1.11.0
 mattb  (e) pytest  ~  misc  pytest-mark  vim test_foo.py
 mattb  (e) pytest  ~  misc  pytest-mark  pytest .
================================================= test session starts ==================================================
platform linux -- Python 3.7.1, pytest-4.0.0, py-1.7.0, pluggy-0.8.0 -- /home/mattb/programs/anaconda3/envs/pytest/bin/python
cachedir: .pytest_cache
rootdir: /home/mattb/misc/pytest-mark, inifile:
collected 0 items / 1 errors                                                                                           

======================================================== ERRORS ========================================================
_____________________________________________ ERROR collecting test_foo.py _____________________________________________
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/pluggy/hooks.py:284: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/pluggy/manager.py:67: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/pluggy/manager.py:61: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/python.py:224: in pytest_pycollect_makeitem
    res = list(collector._genfunctions(name, obj))
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/python.py:409: in _genfunctions
    self.ihook.pytest_generate_tests(metafunc=metafunc)
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/pluggy/hooks.py:284: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/pluggy/manager.py:67: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/pluggy/manager.py:61: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/python.py:133: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs)
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/python.py:942: in parametrize
    function_definition=self.definition,
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/mark/structures.py:127: in _for_parametrize
    for x in argvalues
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/mark/structures.py:127: in <listcomp>
    for x in argvalues
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/mark/structures.py:110: in extract_from
    belonging_definition.warn(MARK_PARAMETERSET_UNPACKING)
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/nodes.py:181: in warn
    self._std_warn(_code_or_warning)
../../programs/anaconda3/envs/pytest/lib/python3.7/site-packages/_pytest/nodes.py:232: in _std_warn
    lineno=lineno + 1 if lineno is not None else None,
E   _pytest.warning_types.RemovedInPytest4Warning: Applying marks directly to parameters is deprecated, please use pytest.param(..., marks=...) instead.
E   For more details, see: https://docs.pytest.org/en/latest/parametrize.html
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================== 1 error in 0.26 seconds ================================================

The documentation shows that keywords can be given for these different marks: https://docs.pytest.org/en/latest/skipping.html#skipping-test-functions
But the suggested solution of using pytest.param doesn't seem to allow users to specify kwargs for the marks: Applying marks directly to parameters is deprecated, please use pytest.param(..., marks=...) instead.

This is in a clean virtual environment where the only difference is the pytest version (shown above). This is also on Ubuntu 16.04.

I really hope there's some way to do this, as it's pretty crucial to one of my favorite plugins pytest-raises

@RonnyPfannschmidt
Copy link
Member

that way to use marks is deprecated since a while now and rightfully errors on pytest 4

instead of pytest.mark.foo(values, ...) use pytest.param(values, marks=pytest.mark.foo(...))

@AetherUnbound
Copy link
Author

Got it - I didn't realize we could omit the values portion from the marks call. Thanks!

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

2 participants