Skip to content

Commit

Permalink
DRY out getting the project version in tests
Browse files Browse the repository at this point in the history
We were previously getting the version in three different tests. It
makes more sense to turn getting the project version into a fixture and
to use that fixture anywhere the project version is needed.
  • Loading branch information
mcdonnnj committed Dec 6, 2024
1 parent 03e668f commit 414efb4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
MAIN_SERVICE_NAME = "example"
VERSION_SERVICE_NAME = f"{MAIN_SERVICE_NAME}-version"

VERSION_FILE = "src/version.txt"


@pytest.fixture(scope="session")
def dockerc():
Expand All @@ -36,6 +38,14 @@ def version_container(dockerc):
return dockerc.compose.ps(services=[VERSION_SERVICE_NAME], all=True)[0]


@pytest.fixture(scope="session")
def project_version():
"""Return the version of the project."""
with open(VERSION_FILE) as f:
project_version = f.read().strip()
return project_version


def pytest_addoption(parser):
"""Add new commandline options to pytest."""
parser.addoption(
Expand Down
12 changes: 3 additions & 9 deletions tests/container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,25 @@ def test_output(dockerc, main_container):
@pytest.mark.skipif(
RELEASE_TAG in [None, ""], reason="this is not a release (RELEASE_TAG not set)"
)
def test_release_version():
def test_release_version(project_version):
"""Verify that release tag version agrees with the module version."""
with open(VERSION_FILE) as f:
project_version = f.read().strip()
assert (
RELEASE_TAG == f"v{project_version}"
), "RELEASE_TAG does not match the project version"


def test_log_version(dockerc, version_container):
def test_log_version(dockerc, project_version, version_container):
"""Verify the container outputs the correct version to the logs."""
# make sure container exited if running test isolated
dockerc.wait(version_container.id)
log_output = version_container.logs().strip()
with open(VERSION_FILE) as f:
project_version = f.read().strip()
assert (
log_output == project_version
), f"Container version output to log does not match project version file {VERSION_FILE}"


def test_container_version_label_matches(version_container):
def test_container_version_label_matches(project_version, version_container):
"""Verify the container version label is the correct version."""
with open(VERSION_FILE) as f:
project_version = f.read().strip()
assert (
version_container.config.labels["org.opencontainers.image.version"]
== project_version
Expand Down

0 comments on commit 414efb4

Please sign in to comment.