question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select tests based on marker with arguments

See original GitHub issue

v5.3.4 on Python 3.7/macOS

In conftest.py:

def pytest_configure(config):
    config.addinivalue_line("markers", "year(arg): this marker takes arguments.")

In test_example.py:

import pytest

@pytest.mark.year(2015)
def test_a():
    assert 2 + 2 == 4

@pytest.mark.year(2020)
def test_b():
    assert 1 + 1 == 2

Cannot figure out how to run tests marked year 2015, the one obvious way gives a crash:

$ pytest -m "year(2015)"
======================================================================= test session starts =======================================================================
platform darwin -- Python 3.7.6, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: /private/tmp
collected 2 items                                                                                                                                                 

====================================================================== no tests ran in 0.04s ======================================================================
Traceback (most recent call last):
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/main.py", line 197, in wrap_session
    session.exitstatus = doit(config, session) or 0
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/main.py", line 246, in _main
    config.hook.pytest_collection(session=session)
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/main.py", line 256, in pytest_collection
    return session.perform_collect()
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/main.py", line 461, in perform_collect
    session=self, config=self.config, items=items
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/mark/__init__.py", line 143, in pytest_collection_modifyitems
    deselect_by_mark(items, config)
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/mark/__init__.py", line 131, in deselect_by_mark
    if matchmark(item, matchexpr):
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/mark/legacy.py", line 72, in matchmark
    return eval(markexpr, {}, MarkMapping.from_item(colitem))
  File "<string>", line 1, in <module>
TypeError: 'bool' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/private/tmp/.venv/bin/pytest", line 8, in <module>
    sys.exit(main())
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 93, in main
    config=config
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/private/tmp/.venv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/main.py", line 240, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/main.py", line 219, in wrap_session
    config.notify_exception(excinfo, config.option)
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 822, in notify_exception
    funcargs=True, showlocals=getattr(option, "showlocals", False), style=style
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/_code/code.py", line 619, in getrepr
    self._getreprcrash(),
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/_code/code.py", line 570, in _getreprcrash
    entry = self.traceback.getcrashentry()
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/_code/code.py", line 383, in getcrashentry
    if not entry.ishidden():
  File "/private/tmp/.venv/lib/python3.7/site-packages/_pytest/_code/code.py", line 264, in ishidden
    tbh = f.f_locals.get(
AttributeError: 'MarkMapping' object has no attribute 'get'

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
nicoddemuscommented, Jun 11, 2020

Sure go ahead. I’ll let you do the honors. 😁

0reactions
symonkcommented, Jun 11, 2020

#7082 closes #6519

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with custom markers — pytest documentation
You can provide one or more node IDs as positional arguments to select only specified tests. This makes it easy to select tests...
Read more >
Pytest run only tests with specific marker attribute
pytest -m works with marker names, not with marker values, e.g. pytest -m key would run all tests with the marker key ....
Read more >
Working with custom markers — pytest documentation
Selecting tests based on their node ID¶. You can provide one or more node IDs as positional arguments to select only specified tests....
Read more >
Pytest - Grouping the Tests
To use markers, we have to import pytest module in the test file. We can define our own marker names to the tests...
Read more >
Pytest – How to Run Tests Efficiently
You can run Pytest with the option -m basic to select the functions that have the marker “basic”. In this example, there are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found