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.

@pytest.mark.focus

See original GitHub issue

pytest vresion 5.3.0

I would like to have a feature where I can say “run only this test” from the source file without needing to open any other file or modify any command-line script.

A possible solution is to have a unique marker, e.g. @pytest.mark.focus that would allow me to achieve a similar effect to -m MARKEXPR, but with two changes:

  • when no test is marked with it, then run all tests,
  • don’t need to specify it on the command line.

There are multiple use-cases where it would be beneficial:

  • My specific use-case: In my local development, when writing python code, I have a script running on my command line that watches file changes, and whenever a file is modified, the script automatically re-runs pytest. Having @pytest.mark.focus allows me to very quickly focus on a specific test: add decorator + hit save button. I don’t have to look for .sh file, modify it…
  • Another use case: When the pytest command is wrapped in another script/tool, and it is not possible or hard to pass command-line options to pytest.

Possible implementation (by @godlygeek):

import pytest

def pytest_collection_modifyitems(items):
    '''If any items are marked 'focus', run only those items.

    This hook is run automatically by pytest.
    '''
    # Find items with the 'focus' mark
    focused = [item for item in items if item.get_closest_marker("focus")]
    # If any have it, replace the full list of collected items with only them
    if focused:
        items[:] = focused

def pytest_configure(config):
    config.addinivalue_line(
        "markers", "focus: If any tests have this mark, run only those"
    )

Alternative implementation: add a command line option --run-all-if-none-marked, but with a better name.

I believe this feature seems simple and useful enough to be added to pytest directly and not to be a plugin.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
The-Compilercommented, Nov 25, 2019

FWIW there’s already the pytest-only plugin which does that.

0reactions
fkondejcommented, Nov 25, 2019

@RonnyPfannschmidt thank you for clarification. I looked for this kind of information, before creating this ticket, but I have missed it. Do you think it is worth to try this concept in core now? The pytest-only plugin looks mature to me.

I will use the plugin for now and wait for it to be added to the core.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python test fixture to run a single test? - Stack Overflow
Say hello to pytest mark. You can create a focus tag, assign to any test case or method and then run tests with...
Read more >
Working with custom markers — pytest documentation
Working with custom markers¶. Here are some examples using the How to mark test functions with attributes mechanism. Marking test functions and selecting...
Read more >
Focusing on pytest - SuperOrbital
Implementing Rspec's filter-run-when-matching in pytest to quickly focus on a set of ... config): """ Focus on tests marked focus, if any.
Read more >
Running Only One Test - Python Testing with pytest [Book]
Running Only One Test One of the first things you'll want to do once you've started writing tests is to run just one....
Read more >
End-To-End Tutorial For Pytest Fixtures With Examples
In this part of the Selenium Python tutorial series, I'll focus on pytest fixtures. I'd further explore why you should use it for...
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