2021-04-04
In this post Peter describes an effective, graceful way of differentiating between unit and integration tests in a Go codebase. While build tags and the standard tooling around tests might be Go related, I believe that the pattern applies to other ecosystems as well. For instance Python and Pytest.
Pytest makes this easy using the skipif mark:
import os
import pytest
def test_unit():
assert True
@pytest.mark.skipif(os.getenv("DB") == None, reason="Env var DB not set")
def test_integration():
assert True
And there’s an even more convenient way with a reusable annotation:
needs_db = pytest.mark.skipif(os.getenv("DB") == None, reason="Env var DB not set")
@needs_db
def test_integration():
assert True
Now running either pytest -v
or pytest -rs
without setting the environment variable hints at why the test is skipped:
test_me.py::test_unit PASSED
test_me.py::test_integration SKIPPED (Env var DB not set)
Overall this seems like a very reasonable way to separate unit from integration tests in a straightforward and discoverable way.
Leave A Comment