Skip to content

Commit

Permalink
Add service markers to avoid slow tests with pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Dec 21, 2021
1 parent 76655f3 commit 8940b59
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@
from .fixtures.database import *


def pytest_configure(config):
config.addinivalue_line(
"markers", "service(arg) a service integration test. For example, 'docker'."
)


def pytest_addoption(parser):
parser.addoption(
"--service",
action="append",
metavar="SERVICE",
default=[],
help="include service integration tests for SERVICE.",
)
parser.addoption(
"--all-services",
action="store_true",
default=False,
help="include all service integration tests",
)


def pytest_runtest_setup(item):
envnames = [mark.args[0] for mark in item.iter_markers(name="env")]
if envnames:
if item.config.getoption("-E") not in envnames:
pytest.skip("test requires env in {!r}".format(envnames))


def pytest_collection_modifyitems(session, config, items):
"""
Modify all tests to automatically and transparently support asyncio
Expand All @@ -21,6 +50,22 @@ def pytest_collection_modifyitems(session, config, items):
):
item.add_marker(pytest.mark.asyncio)

if config.getoption("--all-services"):
# Do not skip any service tests
return

run_services = set(config.getoption("--service"))
for item in items:
item_services = {mark.args[0] for mark in item.iter_markers(name="service")}
missing_services = item_services.difference(run_services)
if missing_services:
item.add_marker(
pytest.mark.skip(
f"Requires services: {', '.join(missing_services)}. "
"Use '--service NAME' to include test."
)
)


@pytest.fixture(scope="session")
def event_loop(request):
Expand Down

0 comments on commit 8940b59

Please sign in to comment.