Skip to content

Instantly share code, notes, and snippets.

@billyshambrook
Last active March 19, 2018 17:26
Show Gist options
  • Save billyshambrook/264624b039fb3caa9278 to your computer and use it in GitHub Desktop.
Save billyshambrook/264624b039fb3caa9278 to your computer and use it in GitHub Desktop.
Categorise integration tests using PyTest.
import pytest
def pytest_addoption(parser):
parser.addoption("--integration", action="store_true", help="run integration tests")
def pytest_runtest_setup(item):
if 'integration' in item.keywords:
item.config.getoption("--integration", skip=True)
import pytest
def test_unit_test():
assert True
@pytest.mark.integration
def test_integration_test():
assert True
@billyshambrook
Copy link
Author

That's great, thanks for the share @tasosval

@masci
Copy link

masci commented Mar 19, 2018

This is what I ended up with, the logic could be very different depending on the use case:

def pytest_addoption(parser):
    parser.addoption("--integration", action="store_true", help="run integration tests")


def pytest_runtest_setup(item):
    """
    Only run tests marked with 'integration' when --integration is passed
    """
    run_integration = item.config.getoption("--integration")

    if run_integration and 'integration' not in item.keywords:
        pytest.skip("skipping test not marked as integration")
    elif 'integration' in item.keywords and not run_integration:
        pytest.skip("pass --integration option to pytest to run this test")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment