Skip to content

Commit 3bafb76

Browse files
authored
Replace Nosetests with Pytest as test runner (nilearn#419)
* Replace nosetests with pytest as test runner * Replaced yield asserts with standard asserts (pytest unsupported) * Replaced nilearn with nistats references * Fixed: Makefile uses tabs not spaces * Moved pytest-cov to always install so --cov option always works * Skip testing reporting module when no matplotlib * Skip testing reporting module when no matplotlib - 2 * Skip testing reporting module when no matplotlib - 3
1 parent 39f789d commit 3bafb76

File tree

7 files changed

+56
-27
lines changed

7 files changed

+56
-27
lines changed

Makefile

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
PYTHON ?= python
66
CYTHON ?= cython
7-
NOSETESTS ?= nosetests
7+
TESTRUNNER ?= nosetests
88
CTAGS ?= ctags
99

1010
all: clean test doc-plot pdf
@@ -29,16 +29,13 @@ inplace:
2929
$(PYTHON) setup.py build_ext -i
3030

3131
test-code:
32-
$(NOSETESTS) -s nistats
32+
python -m pytest --pyargs nistats --cov=nistats
3333
test-doc:
34-
$(NOSETESTS) -s --with-doctest --doctest-tests --doctest-extension=rst \
35-
--doctest-extension=inc --doctest-fixtures=_fixture doc/ \
36-
34+
pytest --doctest-glob='*.rst' `find doc/ -name '*.rst'`
3735

3836
test-coverage:
3937
rm -rf coverage .coverage
40-
$(NOSETESTS) -s --with-coverage --cover-html --cover-html-dir=coverage \
41-
--cover-package=nistats nistats
38+
pytest --pyargs nistats --showlocals --cov=nistats --cov-report=html:coverage
4239

4340
test: test-code test-doc
4441

continuous_integration/install.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ create_new_venv() {
2222
deactivate
2323
virtualenv --system-site-packages testvenv
2424
source testvenv/bin/activate
25-
pip install nose
25+
pip install nose pytest pytest-cov
2626
}
2727

2828
print_conda_requirements() {
@@ -33,7 +33,7 @@ print_conda_requirements() {
3333
# if yes which version to install. For example:
3434
# - for numpy, NUMPY_VERSION is used
3535
# - for scikit-learn, SCIKIT_LEARN_VERSION is used
36-
TO_INSTALL_ALWAYS="pip nose libgfortran=3.0=0 nomkl"
36+
TO_INSTALL_ALWAYS="pip nose pytest pytest-cov libgfortran=3.0=0 nomkl"
3737
REQUIREMENTS="$TO_INSTALL_ALWAYS"
3838
TO_INSTALL_MAYBE="python numpy scipy matplotlib scikit-learn pandas"
3939
for PACKAGE in $TO_INSTALL_MAYBE; do

nistats/conftest.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from distutils.version import LooseVersion
2+
3+
import numpy as np
4+
import pytest
5+
from _pytest.doctest import DoctestItem
6+
7+
try:
8+
import matplotlib
9+
except ImportError:
10+
collect_ignore = ['reporting',
11+
'tests/test_glm_reporter.py',
12+
'tests/test_reporting.py',
13+
'tests/test_sphinx_report.py',
14+
]
15+
else:
16+
matplotlib # Prevents flake8 erring due to unused entities.
17+
18+
19+
def pytest_collection_modifyitems(items):
20+
# numpy changed the str/repr formatting of numpy arrays in 1.14. We want to
21+
# run doctests only for numpy >= 1.14.Adapted from scikit-learn
22+
if LooseVersion(np.__version__) < LooseVersion('1.14'):
23+
reason = 'doctests are only run for numpy >= 1.14'
24+
skip_doctests = True
25+
else:
26+
skip_doctests = False
27+
28+
if skip_doctests:
29+
skip_marker = pytest.mark.skip(reason=reason)
30+
for item in items:
31+
if isinstance(item, DoctestItem):
32+
item.add_marker(skip_marker)

nistats/reporting/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from ._get_clusters_table import get_clusters_table
2121
from ._plot_matrices import plot_contrast_matrix, plot_design_matrix
2222
from .glm_reporter import make_glm_report
23-
from.sphinx_report import _ReportScraper
23+
from .sphinx_report import _ReportScraper
2424

2525
__all__ = [compare_niimgs,
2626
get_clusters_table,

nistats/tests/test_utils.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,18 @@ def test_pos_recipr():
122122
X = np.array([2, 1, -1, 0], dtype=np.int8)
123123
eX = np.array([0.5, 1, 0, 0])
124124
Y = positive_reciprocal(X)
125-
yield assert_array_almost_equal, Y, eX
126-
yield assert_equal, Y.dtype.type, np.float64
125+
assert_array_almost_equal, Y, eX
126+
assert_equal, Y.dtype.type, np.float64
127127
X2 = X.reshape((2, 2))
128128
Y2 = positive_reciprocal(X2)
129-
yield assert_array_almost_equal, Y2, eX.reshape((2, 2))
129+
assert_array_almost_equal, Y2, eX.reshape((2, 2))
130130
# check that lists have arrived
131131
XL = [0, 1, -1]
132-
yield assert_array_almost_equal, positive_reciprocal(XL), [0, 1, 0]
132+
assert_array_almost_equal, positive_reciprocal(XL), [0, 1, 0]
133133
# scalars
134-
yield assert_equal, positive_reciprocal(-1), 0
135-
yield assert_equal, positive_reciprocal(0), 0
136-
yield assert_equal, positive_reciprocal(2), 0.5
134+
assert_equal, positive_reciprocal(-1), 0
135+
assert_equal, positive_reciprocal(0), 0
136+
assert_equal, positive_reciprocal(2), 0.5
137137

138138

139139
def test_img_table_checks():

requirements-dev.txt

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ scipy
55
scikit-learn
66
pandas
77
matplotlib
8+
pytest
9+
pytest-cov

setup.cfg

+8-10
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
[bdist_rpm]
44
doc-files = doc
55

6-
[nosetests]
7-
verbosity = 2
8-
detailed-errors = 1
9-
with-coverage = 1
10-
cover-package = nistats
11-
#pdb = 1
12-
#pdb-failures = 1
13-
with-doctest=1
14-
doctest-extension=rst
15-
ignore-files=plot_.*.py
6+
[tool:pytest]
7+
doctest_optionflags = NORMALIZE_WHITESPACE ELLIPSIS
8+
junit_family = xunit2
9+
addopts =
10+
--doctest-modules
11+
-s
12+
-vv
13+
--durations=0

0 commit comments

Comments
 (0)