Skip to content

Commit

Permalink
test: update executable and module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LennyN95 committed Apr 24, 2024
1 parent e58b601 commit 2be007b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 39 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ jobs:

- name: Install dependencies
run: |
pip install pytest pytest-cov
# pip install pytest pytest-cov
# Waiting pip supports `--only-deps=test`, explicitly extract the test dependencies
# See https://github.com/pypa/pip/issues/11440
pip install yq
tomlq -r '.project."optional-dependencies".test[]' pyproject.toml | xargs -d '\n' pip install
- uses: actions/download-artifact@v4
with:
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies = []

[project.optional-dependencies]
test = [
"importlib_metadata>=2.0; python_version<'3.10'",
"pytest >=6",
"pytest-cov >=3",
]
Expand Down Expand Up @@ -116,6 +117,9 @@ module = "dcmqi.*"
disallow_untyped_defs = true
disallow_incomplete_defs = true

[[tool.mypy.overrides]]
module = "tests.*"
ignore_errors = true

[tool.ruff]
src = ["src"]
Expand Down
12 changes: 12 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations

import sys
from contextlib import contextmanager


@contextmanager
def push_argv(argv):
old_argv = sys.argv
sys.argv = argv
yield
sys.argv = old_argv
60 changes: 60 additions & 0 deletions tests/test_executable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import subprocess
import sys
import sysconfig
from pathlib import Path

import pytest

if sys.version_info < (3, 10):
from importlib_metadata import distribution
else:
from importlib.metadata import distribution

import dcmqi

from . import push_argv

all_tools_version = pytest.mark.parametrize("tool,expected_version", [
("itkimage2segimage", "1.0"),
("segimage2itkimage", "1.0"),
("tid1500writer", "1.0"),
("tid1500reader", "1.0"),
("itkimage2paramap", "1.0"),
("paramap2itkimage", "1.0")]
)

def _get_scripts():
dist = distribution("dcmqi")
scripts_paths = [
Path(sysconfig.get_path("scripts", scheme)).resolve()
for scheme in sysconfig.get_scheme_names()
]
scripts = []
for file in dist.files:
if file.locate().parent.resolve(strict=True) in scripts_paths:
scripts.append(file.locate().resolve(strict=True))
return scripts


@all_tools_version
def test_package_script(tool, expected_version):
"""
Example of the output for tid1500writer (analogous for the other tools):
tid1500writer --version
dcmqi repository URL: https://github.com/QIICR/dcmqi revision: 1922a09 tag: v1.3.1
/home/leonard/miniconda3/envs/dcmqi_pypi_test/lib/python3.8/site-packages/dcmqi/bin/tid1500writer version: 1.0
"""
scripts = [script for script in _get_scripts() if script.stem == tool]
assert len(scripts) == 1
output = subprocess.check_output([str(scripts[0]), "--version"]).decode("ascii")
assert output.splitlines()[2].split(" ")[1] == f"version: {expected_version}"

@all_tools_version
def test_module(tool, expected_version):
func = getattr(dcmqi, tool)
args = [f"{tool}.py", "--version"]
with push_argv(args), pytest.raises(SystemExit) as excinfo:
func()
assert excinfo.value.code == 0
38 changes: 0 additions & 38 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,8 @@

import importlib.metadata

import pytest

import dcmqi as m


def test_version():
assert importlib.metadata.version("dcmqi") == m.__version__


def test_itkimage2segimage():
with pytest.raises(SystemExit) as e:
m.itkimage2segimage()
assert e.value.code == 1


def test_segimage2itkimage():
with pytest.raises(SystemExit) as e:
m.segimage2itkimage()
assert e.value.code == 1


def test_tid1500writer():
with pytest.raises(SystemExit) as e:
m.tid1500writer()
assert e.value.code == 1


def test_tid1500reader():
with pytest.raises(SystemExit) as e:
m.tid1500reader()
assert e.value.code == 1


def test_itkimage2paramap():
with pytest.raises(SystemExit) as e:
m.itkimage2paramap()
assert e.value.code == 1


def test_paramap2itkimage():
with pytest.raises(SystemExit) as e:
m.paramap2itkimage()
assert e.value.code == 1

0 comments on commit 2be007b

Please sign in to comment.