Eric J Ma's Website

Annotating code tests and selectively running tests

written by Eric J. Ma on 2018-02-25 | tags: programming python testing software engineering

I just learned about a neat trick when using pytest - the ability to "mark" tests with metadata, and the ability to selectively run groups of marked tests.

Here's an example:

import pytest

@pytest.mark.slow  # annotate it as a "slow" test
def test_that_runs_slowly():

@pytest.mark.slow  # annotate test as a "slow" test.
@pytest.mark.integration  # annotate test as being an "integration" test
def test_that_does_integration():

What's really cool here is that I can selectively run slow tests or selectively run integration tests:

$ py.test -m "slow"   # only runs "slow" tests
$ py.test -m "integration"  # only runs "integration" tests
$ py.test -m "not integration"  # only runs tests that are not "integration" tests.

I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.

If you would like to receive deeper, in-depth content as an early subscriber, come support me on Patreon!