Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use pytest for tests #1405

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
}
Expand All @@ -58,7 +58,7 @@ pipeline {
export CAIMAN_DATA=$TEMPDIR/caiman_data
cd $TEMPDIR
caimanmanager.py install
nosetests --traverse-namespace caiman
pytest --pyargs caiman
'''
}
}
Expand Down
22 changes: 11 additions & 11 deletions caiman/caimanmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
56 changes: 27 additions & 29 deletions caiman/tests/test_memmapping.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion environment-minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
- ipywidgets
- matplotlib
- moviepy
- nose
- pytest
- numpy <2.0.0,>=1.26
- numpydoc
- opencv
Expand Down
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ dependencies:
- matplotlib
- moviepy
- mypy
- nose
- pytest
- pytest-cov
- numpy <2.0.0,>=1.26
- numpydoc
- opencv
Expand Down