From 021886627a207dba1a81e39c3ea97b9f240b1bf3 Mon Sep 17 00:00:00 2001 From: Mark Harfouche Date: Sun, 15 Sep 2024 18:59:08 -0400 Subject: [PATCH] Try to use pytest for tests --- Jenkinsfile | 4 +-- caiman/caimanmanager.py | 22 ++++++------- caiman/tests/test_memmapping.py | 56 ++++++++++++++++----------------- environment-minimal.yml | 2 +- environment.yml | 3 +- 5 files changed, 43 insertions(+), 44 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 92e5c8506..4c4fff6e5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -34,7 +34,7 @@ pipeline { export THEANO_FLAGS="base_compiledir=$TEMPDIR/theano_tmp" cd $TEMPDIR caimanmanager.py install - nosetests --traverse-namespace caiman + pytest --pyargs caiman caimanmanager.py demotest ''' } @@ -58,7 +58,7 @@ pipeline { export CAIMAN_DATA=$TEMPDIR/caiman_data cd $TEMPDIR caimanmanager.py install - nosetests --traverse-namespace caiman + pytest --pyargs caiman ''' } } diff --git a/caiman/caimanmanager.py b/caiman/caimanmanager.py index 62580930c..968bbfca6 100755 --- a/caiman/caimanmanager.py +++ b/caiman/caimanmanager.py @@ -125,31 +125,31 @@ def do_check_install(targdir: str, inplace: bool = False) -> None: raise Exception("Install is dirty") -def do_run_nosetests(targdir: str) -> None: - out, err, ret = runcmd(["nosetests", "--verbose", "--traverse-namespace", "caiman"]) +def do_run_pytest(targdir: str) -> None: + out, err, ret = runcmd(["pytest", "--verbose", "--pyargs", "caiman"]) if ret != 0: - print(f"Nosetests failed with return code {ret}") + print(f"pytest failed with return code {ret}") sys.exit(ret) else: - print("Nosetests success!") + print("pytest success!") -def do_run_coverage_nosetests(targdir: str) -> None: - # Run nosetests, but invoke coverage so we get statistics on how much our tests actually exercise +def do_run_coverage_pytest(targdir: str) -> None: + # Run pytest, but invoke coverage so we get statistics on how much our tests actually exercise # the code. It would probably be a mistake to do CI testing around these figures (as we often add things to # the codebase before they're fully fleshed out), but we can at least make the command below easier to invoke # with this frontend. # # This command will not function from the conda package, because there would be no reason to use it in that case. # If we ever change our mind on this, it's a simple addition of the coverage package to the feedstock. - out, err, ret = runcmd(["nosetests", "--verbose", "--with-coverage", "--cover-package=caiman", "--cover-erase", "--traverse-namespace", "caiman"]) + out, err, ret = runcmd(["pytest", "--verbose", "--cov=caiman", "caiman"]) if ret != 0: - print("Nosetests failed with return code " + str(ret)) + print("pytestfailed with return code " + str(ret)) print("If it failed due to a message like the following, it is a known issue:") print("ValueError: cannot resize an array that references or is referenced by another array in this way.") print("We believe this to be harmless and caused by coverage having additional rules for code") sys.exit(ret) else: - print("Nosetests success!") + print("pytest success!") def do_run_demotests(targdir: str) -> None: @@ -255,9 +255,9 @@ def main(): elif cfg.command == 'check': do_check_install(cfg.userdir, cfg.inplace) elif cfg.command == 'test': - do_run_nosetests(cfg.userdir) + do_run_pytest(cfg.userdir) elif cfg.command == 'covtest': - do_run_coverage_nosetests(cfg.userdir) + do_run_coverage_pytest(cfg.userdir) elif cfg.command == 'demotest': if os.name == 'nt': do_nt_run_demotests(cfg.userdir) diff --git a/caiman/tests/test_memmapping.py b/caiman/tests/test_memmapping.py index 6a3e6df9f..99d1eb7f9 100644 --- a/caiman/tests/test_memmapping.py +++ b/caiman/tests/test_memmapping.py @@ -1,22 +1,12 @@ import pathlib import numpy as np -import nose +import pytest from caiman import mmapping from caiman.paths import caiman_datadir -TWO_D_FNAME = ( - pathlib.Path(caiman_datadir()) - / "testdata/memmap__d1_10_d2_11_d3_1_order_F_frames_12_.mmap" -) -THREE_D_FNAME = ( - pathlib.Path(caiman_datadir()) - / "testdata/memmap__d1_10_d2_11_d3_13_order_F_frames_12_.mmap" -) - - def test_load_raises_wrong_ext(): fname = "a.mmapp" try: @@ -37,38 +27,46 @@ def test_load_raises_multiple_ext(): assert False -def setup_2d_mmap(): - np.memmap( - TWO_D_FNAME, mode="w+", dtype=np.float32, shape=(12, 10, 11, 13), order="F" +@pytest.fixture(scope="function") +def three_d_mmap_fname(): + THREE_D_FNAME = ( + pathlib.Path(caiman_datadir()) + / "testdata/memmap__d1_10_d2_11_d3_13_order_F_frames_12_.mmap" ) - - -def teardown_2d_mmap(): - TWO_D_FNAME.unlink() - - -def setup_3d_mmap(): np.memmap( THREE_D_FNAME, mode="w+", dtype=np.float32, shape=(12, 10, 11, 13), order="F" ) + try: + yield THREE_D_FNAME + finally: + THREE_D_FNAME.unlink() -def teardown_3d_mmap(): - THREE_D_FNAME.unlink() +@pytest.fixture(scope="function") +def two_d_mmap_fname(): + TWO_D_FNAME = ( + pathlib.Path(caiman_datadir()) + / "testdata/memmap__d1_10_d2_11_d3_1_order_F_frames_12_.mmap" + ) + np.memmap( + TWO_D_FNAME, mode="w+", dtype=np.float32, shape=(12, 10, 11, 13), order="F" + ) + try: + yield TWO_D_FNAME + finally: + TWO_D_FNAME.unlink() -@nose.with_setup(setup_2d_mmap, teardown_2d_mmap) -def test_load_successful_2d(): - fname = pathlib.Path(caiman_datadir()) / "testdata" / TWO_D_FNAME +def test_load_successful_2d(two_d_mmap_fname): + fname = two_d_mmap_fname Yr, (d1, d2), T = mmapping.load_memmap(str(fname)) assert (d1, d2) == (10, 11) assert T == 12 assert isinstance(Yr, np.memmap) -@nose.with_setup(setup_3d_mmap, teardown_3d_mmap) -def test_load_successful_3d(): - fname = pathlib.Path(caiman_datadir()) / "testdata" / THREE_D_FNAME +def test_load_successful_3d(three_d_mmap_fname): + fname = three_d_mmap_fname Yr, (d1, d2, d3), T = mmapping.load_memmap(str(fname)) assert (d1, d2, d3) == (10, 11, 13) assert T == 12 diff --git a/environment-minimal.yml b/environment-minimal.yml index e2f6f7f81..31dfbef4e 100644 --- a/environment-minimal.yml +++ b/environment-minimal.yml @@ -12,7 +12,7 @@ dependencies: - ipywidgets - matplotlib - moviepy -- nose +- pytest - numpy <2.0.0,>=1.26 - numpydoc - opencv diff --git a/environment.yml b/environment.yml index 6394dbbb1..3ac8a95bc 100644 --- a/environment.yml +++ b/environment.yml @@ -17,7 +17,8 @@ dependencies: - matplotlib - moviepy - mypy -- nose +- pytest +- pytest-cov - numpy <2.0.0,>=1.26 - numpydoc - opencv