From 5f9ebbea1bc657f7d8748b4b35b2e3c745078b07 Mon Sep 17 00:00:00 2001 From: Uku Loskit Date: Tue, 9 Jul 2024 08:40:54 +0300 Subject: [PATCH 1/3] Convert tests to use pytest instead of unittest --- .github/workflows/tests.yml | 4 ++-- examples/test_compare.py | 10 +++++----- examples/test_kinematic_kf.py | 13 ++++++++----- requirements.txt | 1 + 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0e09120..9b0f82d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,8 +17,8 @@ jobs: - name: Static analysis run: ${{ env.RUN }} "git init && git add -A && pre-commit run --all" - name: Unit Tests - run: ${{ env.RUN }} "cd /project/examples; python -m unittest discover" - + run: ${{ env.RUN }} "pytest" + docker_push: name: docker push runs-on: ubuntu-latest diff --git a/examples/test_compare.py b/examples/test_compare.py index 0b2cdd3..291f29e 100755 --- a/examples/test_compare.py +++ b/examples/test_compare.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 +import pytest import os import sys import sympy as sp import numpy as np -import unittest if __name__ == '__main__': # generating sympy code from rednose.helpers.ekf_sym import gen_code @@ -83,7 +83,7 @@ def get_R(self, kind, n): return R -class TestCompare(unittest.TestCase): +class TestCompare: def test_compare(self): np.random.seed(0) @@ -115,9 +115,9 @@ def test_compare(self): kf.filter_py.predict_and_update_batch(t, ObservationKind.POSITION, z, R) kf.filter_pyx.predict_and_update_batch(t, ObservationKind.POSITION, z, R) - self.assertAlmostEqual(kf.filter_py.get_filter_time(), kf.filter_pyx.get_filter_time()) - self.assertTrue(np.allclose(kf.filter_py.state(), kf.filter_pyx.state())) - self.assertTrue(np.allclose(kf.filter_py.covs(), kf.filter_pyx.covs())) + assert kf.filter_py.get_filter_time() == pytest.approx(kf.filter_pyx.get_filter_time()) + assert np.allclose(kf.filter_py.state(), kf.filter_pyx.state()) + assert np.allclose(kf.filter_py.covs(), kf.filter_pyx.covs()) if __name__ == "__main__": diff --git a/examples/test_kinematic_kf.py b/examples/test_kinematic_kf.py index 749eaf0..8897de3 100644 --- a/examples/test_kinematic_kf.py +++ b/examples/test_kinematic_kf.py @@ -1,12 +1,15 @@ +import pytest import os import numpy as np import unittest +import sys +sys.path.append(os.path.dirname(__file__)) from kinematic_kf import KinematicKalman, ObservationKind, States # pylint: disable=import-error GENERATED_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'generated')) -class TestKinematic(unittest.TestCase): +class TestKinematic: def test_kinematic_kf(self): np.random.seed(0) @@ -49,10 +52,10 @@ def test_kinematic_kf(self): xs, xs_meas, xs_kf, vs_kf, xs_kf_std, vs_kf_std = (np.asarray(a) for a in (xs, xs_meas, xs_kf, vs_kf, xs_kf_std, vs_kf_std)) - self.assertAlmostEqual(xs_kf[-1], -0.010866289677966417) - self.assertAlmostEqual(xs_kf_std[-1], 0.04477103863330089) - self.assertAlmostEqual(vs_kf[-1], -0.8553720537261753) - self.assertAlmostEqual(vs_kf_std[-1], 0.6695762270974388) + assert xs_kf[-1] == pytest.approx(-0.010866289677966417) + assert xs_kf_std[-1] == pytest.approx(0.04477103863330089) + assert vs_kf[-1] == pytest.approx(-0.8553720537261753) + assert vs_kf_std[-1] == pytest.approx(0.6695762270974388) if "PLOT" in os.environ: import matplotlib.pyplot as plt # pylint: disable=import-error diff --git a/requirements.txt b/requirements.txt index 453554b..6f9e082 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ cffi scons pre-commit Cython +pytest From 2cf7434271debee5e6fceb812bd8bd64dd9bd889 Mon Sep 17 00:00:00 2001 From: Uku Loskit Date: Tue, 9 Jul 2024 22:33:40 +0300 Subject: [PATCH 2/3] Augment PYTHONPATH for pytest via pyproject.toml --- examples/test_kinematic_kf.py | 2 -- pyproject.toml | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/test_kinematic_kf.py b/examples/test_kinematic_kf.py index 8897de3..941a2c7 100644 --- a/examples/test_kinematic_kf.py +++ b/examples/test_kinematic_kf.py @@ -2,8 +2,6 @@ import os import numpy as np import unittest -import sys -sys.path.append(os.path.dirname(__file__)) from kinematic_kf import KinematicKalman, ObservationKind, States # pylint: disable=import-error diff --git a/pyproject.toml b/pyproject.toml index b417c70..fc2f569 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,3 +7,6 @@ target-version="py311" select = ["E", "F", "W", "PIE", "C4", "ISC", "RUF100", "A"] ignore = ["W292", "E741", "E402", "C408", "ISC003"] flake8-implicit-str-concat.allow-multiline=false + +[tool.pytest.ini_options] +pythonpath = "examples" From ca5c6b2848d017ce37f62a94c188f4eec4ab8285 Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 9 Jul 2024 17:30:21 -0700 Subject: [PATCH 3/3] cleanup --- examples/test_kinematic_kf.py | 7 +------ pyproject.toml | 6 +++++- requirements.txt | 1 + 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/test_kinematic_kf.py b/examples/test_kinematic_kf.py index 941a2c7..05d4d3e 100644 --- a/examples/test_kinematic_kf.py +++ b/examples/test_kinematic_kf.py @@ -1,9 +1,8 @@ import pytest import os import numpy as np -import unittest -from kinematic_kf import KinematicKalman, ObservationKind, States # pylint: disable=import-error +from .kinematic_kf import KinematicKalman, ObservationKind, States GENERATED_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'generated')) @@ -81,7 +80,3 @@ def test_kinematic_kf(self): plt.legend() plt.show() - - -if __name__ == "__main__": - unittest.main() diff --git a/pyproject.toml b/pyproject.toml index fc2f569..fa6ae11 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,5 +8,9 @@ select = ["E", "F", "W", "PIE", "C4", "ISC", "RUF100", "A"] ignore = ["W292", "E741", "E402", "C408", "ISC003"] flake8-implicit-str-concat.allow-multiline=false +[tool.ruff.lint.flake8-tidy-imports.banned-api] +"pytest.main".msg = "pytest.main requires special handling that is easy to mess up!" +"unittest".msg = "Use pytest" + [tool.pytest.ini_options] -pythonpath = "examples" +addopts = "--durations=10 -n auto" diff --git a/requirements.txt b/requirements.txt index 6f9e082..81875a8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ scons pre-commit Cython pytest +pytest-xdist