|
| 1 | +import nibabel as nib |
| 2 | +from pkg_resources import resource_filename |
| 3 | +import pytest |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +@pytest.mark.parametrize("verbose, v_args", [(-2, ["-qq"]), |
| 7 | + (-1, ["-q"]), |
| 8 | + (0, []), |
| 9 | + (1, ["-v"]), |
| 10 | + (2, ["-vv"])]) |
| 11 | +@pytest.mark.parametrize("doctests", (True, False)) |
| 12 | +@pytest.mark.parametrize("coverage", (True, False)) |
| 13 | +def test_nibabel_test(verbose, v_args, doctests, coverage): |
| 14 | + expected_args = v_args + ["--doctest-modules", "--cov", "nibabel", "--pyargs", "nibabel"] |
| 15 | + if not doctests: |
| 16 | + expected_args.remove("--doctest-modules") |
| 17 | + if not coverage: |
| 18 | + expected_args[-4:-2] = [] |
| 19 | + |
| 20 | + with mock.patch("pytest.main") as pytest_main: |
| 21 | + nib.test(verbose=verbose, doctests=doctests, coverage=coverage) |
| 22 | + |
| 23 | + args, kwargs = pytest_main.call_args |
| 24 | + assert args == () |
| 25 | + assert kwargs == {"args": expected_args} |
| 26 | + |
| 27 | + |
| 28 | +def test_nibabel_test_errors(): |
| 29 | + with pytest.raises(NotImplementedError): |
| 30 | + nib.test(label="fast") |
| 31 | + with pytest.raises(NotImplementedError): |
| 32 | + nib.test(raise_warnings=[]) |
| 33 | + with pytest.raises(NotImplementedError): |
| 34 | + nib.test(timer=True) |
| 35 | + with pytest.raises(ValueError): |
| 36 | + nib.test(verbose="-v") |
| 37 | + |
| 38 | + |
| 39 | +def test_nibabel_bench(): |
| 40 | + expected_args = ["-c", "--pyargs", "nibabel"] |
| 41 | + |
| 42 | + try: |
| 43 | + expected_args.insert(1, resource_filename("nibabel", "benchmarks/pytest.benchmark.ini")) |
| 44 | + except: |
| 45 | + raise unittest.SkipTest("Not installed") |
| 46 | + |
| 47 | + with mock.patch("pytest.main") as pytest_main: |
| 48 | + nib.bench(verbose=0) |
| 49 | + |
| 50 | + args, kwargs = pytest_main.call_args |
| 51 | + assert args == () |
| 52 | + assert kwargs == {"args": expected_args} |
| 53 | + |
| 54 | + with mock.patch("pytest.main") as pytest_main: |
| 55 | + nib.bench(verbose=0, extra_argv=[]) |
| 56 | + |
| 57 | + args, kwargs = pytest_main.call_args |
| 58 | + assert args == () |
| 59 | + assert kwargs == {"args": expected_args} |
0 commit comments