Pytest: Deselecting tests

With pytest, one can mark tests using a decorator

def some_slow_test():

Then, from the command line, one can tell pytest to skip the tests marked "slow"

pytest -k-slow

If I have an additional tag:

def some_long_test()

I would like to be able to skip both long AND slow tests. I've tried this:

pytest -k-slow -k-long

and this:

pytest -k-slow,long

And neither seems to work.

At the command line, how do I tell pytest to skip both the slow AND the long tests?

Additionally, with the recent addition of the "-m" command line option you should be able to write:

py.test -m "not (slow or long)"

IOW, the "-m" option accepts an expression which can make use of markers as boolean values (if a marker does not exist on a test function it's value is False, if it exists, it is True).

Changing standard (Python) test discovery — pytest documentation, Deselect tests during test collection�. Tests can individually be deselected during collection by passing the --deselect=item option. For example, say tests/� pytest always collects all the tests it can find before running them, so pytest-randomly shuffles that list of tests just before execution. This is a great way to uncover tests that depend on running in a specific order, which means they have a stateful dependency on some other test.

Looking through the pytest code ( and further experimentation shows the following seems to work:

pytest -k "-slow -long"

(Using the --collect-only option speeds up experimentation)

Working with custom markers — pytest documentation, Here are some examples using the Marking test functions with attributes collected 4 items / 3 deselected / 1 selected PASSED� Pytest is a testing framework based on python. It is mainly used to write API test cases. This tutorial helps you understand − This tutorial is designed to benefit IT professionals and students who want to take a step further in their QA Automation career by adding a strong testing framework to

It's also possible to stack the mark decorators.

def test_myfunction():

I then called py.test -m "slow and main" and only the tests with both decorators were called.

py.test -m "not (slow and main)" resulted in the other tests running

show deselected info after collected info and before test execution , 26 -- pytest-2.9.1 -- /usr/bin/python collected 4 items (3 deselected by "-m 'fast'") . * pytest: fix collection warnings via __test__=False Fixes > "cannot collect test class %r because it has a __init__ constructor Ref: pytest-dev/pytest#2007 * pytest: configure testpaths This is faster with test collection.

If you are trying to run the tests from inside a python file, that is, you run your tests by calling

$ python

which has contents

import pytest


and you want to know how to pass the CLI flag in to pytest.main, the answer is:

pytest.main(["-m", "not slow"])

PS - yes, there are legitimate reasons to call tests this way. Pray you never have to learn them.

--deselect does not work with tests inside classes � Issue #4127 , I can backlog an item to investigate this, but I suspect someone who is much more familiar with pytest internals would be much faster at it. The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. An example of a simple test: # content of def inc (x): return x + 1 def test_answer (): assert inc (3) == 5. To execute it:

[PDF] PyTest Guide, py.test : it runs all the tests in the directory, which start with name 'test' or classes which start with name. 'Test'. 3 passed, 2 deselected in 0.11 seconds =====. For test automation using pytest with Selenium WebDriver, you need to install pytest separately, it is more popular than unittest framework, below are some of the advantages of the Pytest framework Can be used by development teams, test teams, teams that are practising Test Driven Development(TDD), as well as in open-source projects.

pytest-select � PyPI, cat selection.txt test_something test_parametrized[1] tests/ $~ pytest --select-from-file selection.txt $~ pytest --deselect-from-file selection.txt� py.test tests_directory/ tests_directory/ -k 'test_001 or test_some_other_test' This will run test cases with name test_001 and test_some_other_test deselecting rest test cases. Note: This will select any test case starting with test_001 or test_some_other_test. Eg if you have test case test_case0012 it will also be selected

pytest: Only run tests using a particular fixture, pytest: Only run tests using a particular fixture 1 deselected in 0.01 seconds == ===== (venv) $ pytest -v --usesfixture bar� Skip and xfail: dealing with tests that cannot succeed. Skipping test functions; XFail: mark test functions as expected to fail; Skip/xfail with parametrize; Parametrizing fixtures and test functions. @pytest.mark.parametrize: parametrizing test functions; Basic pytest_generate_tests example; More examples; Cache: working with cross-testrun

Python Tutorial: Testing with Pytest, Test files which pytest will use for testing have to start with test_ or end with _test. py We will Such a marker can be used to select or deselect test functions. In test_timedistance_v0, we let pytest generate the test IDs. In test_timedistance_v1, we specified ids as a list of strings which were used as the test IDs. These are succinct, but can be a pain to maintain. In test_timedistance_v2, we specified ids as a function that can generate a string representation to make part of the test ID.

  • The important part here is to understand the difference between -m and -k. -m is used to filter on markers, while -k is used to filter on test names.
  • Perhaps the API has changed. I tried pytest -k "-slow -long" and got back py.test: error: argument -k: expected one argument