Skip to content

Commit 6609fec

Browse files
author
Joao Felipe Rocha
committed
Updated the package to use pytest instead of nose2
1 parent 5bd6c24 commit 6609fec

File tree

14 files changed

+64
-42
lines changed

14 files changed

+64
-42
lines changed

.github/workflows/run_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
6161
- name: Run tests
6262
run: |
63-
nose2 -vvv
63+
pytest
6464
6565
- name: Coveralls
6666
env:

pytest.ini

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[pytest]
2+
testpaths = test
3+
python_files = test_*.py
4+
python_classes = Test*
5+
python_functions = test_*
6+
addopts =
7+
-v
8+
--strict-markers
9+
--tb=short
10+
markers =
11+
slow: marks tests as slow (deselect with '-m "not slow"')
12+
filterwarnings =
13+
error
14+
ignore::PendingDeprecationWarning:.*matrix subclass.*
15+
ignore::DeprecationWarning:.*SafeConfigParser.*
16+
ignore::DeprecationWarning:.*collections.*
17+
ignore::DeprecationWarning:.*check_pickle.*

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ exclude =
2727
profile = black
2828
force_single_line = true
2929
force_alphabetical_sort = true
30+
31+
[coverage]
32+
always-on = True
33+
coverage = graphtools

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
]
1515

1616
test_requires = [
17-
"nose",
18-
"nose2",
17+
"pytest",
18+
"pytest-cov",
1919
"pandas",
2020
"coverage",
2121
"coveralls",
@@ -64,7 +64,6 @@
6464
"fast": fast_requires,
6565
"all": all_requires,
6666
},
67-
test_suite="nose2.collector.collector",
6867
long_description=readme,
6968
url="https://github.com/KrishnaswamyLab/graphtools",
7069
download_url="https://github.com/KrishnaswamyLab/graphtools/archive/v{}.tar.gz".format(

test/load_tests/__init__.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from nose.tools import assert_raises_regex
2-
from nose.tools import assert_warns_regex
31
from scipy.spatial.distance import cdist
42
from scipy.spatial.distance import pdist
53
from scipy.spatial.distance import squareform
@@ -8,23 +6,35 @@
86
from sklearn.decomposition import TruncatedSVD
97

108
import graphtools
11-
import nose2
129
import numpy as np
1310
import pandas as pd
1411
import pygsp
12+
import pytest
1513
import re
1614
import scipy.sparse as sp
1715
import warnings
1816

1917

2018
def assert_warns_message(expected_warning, expected_message, *args, **kwargs):
2119
expected_regex = re.escape(expected_message)
22-
return assert_warns_regex(expected_warning, expected_regex, *args, **kwargs)
20+
if args:
21+
# If function arguments are provided, call the function within the context
22+
with pytest.warns(expected_warning, match=expected_regex):
23+
return args[0](*args[1:], **kwargs)
24+
else:
25+
# Return the context manager to be used with 'with' statement
26+
return pytest.warns(expected_warning, match=expected_regex)
2327

2428

2529
def assert_raises_message(expected_warning, expected_message, *args, **kwargs):
2630
expected_regex = re.escape(expected_message)
27-
return assert_raises_regex(expected_warning, expected_regex, *args, **kwargs)
31+
if args:
32+
# If function arguments are provided, call the function within the context
33+
with pytest.raises(expected_warning, match=expected_regex):
34+
return args[0](*args[1:], **kwargs)
35+
else:
36+
# Return the context manager to be used with 'with' statement
37+
return pytest.raises(expected_warning, match=expected_regex)
2838

2939

3040
def reset_warnings():

test/test_data.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
from load_tests import build_graph
66
from load_tests import data
77
from load_tests import graphtools
8-
from load_tests import nose2
98
from load_tests import np
109
from load_tests import pd
1110
from load_tests import pdist
1211
from load_tests import sp
1312
from load_tests import squareform
14-
from nose.tools import assert_raises_regex
1513

1614
import numbers
15+
import pytest
1716
import warnings
1817

1918
try:
@@ -99,9 +98,9 @@ def test_negative_rank_threshold():
9998

10099

101100
def test_True_n_pca_large_threshold():
102-
with assert_raises_regex(
101+
with pytest.raises(
103102
ValueError,
104-
r"Supplied threshold ([0-9\.]*) was greater than maximum singular value ([0-9\.]*) for the data matrix",
103+
match=r"Supplied threshold ([0-9\.]*) was greater than maximum singular value ([0-9\.]*) for the data matrix",
105104
):
106105
build_graph(data, n_pca=True, rank_threshold=np.linalg.norm(data) ** 2)
107106

@@ -388,10 +387,10 @@ def test_inverse_transform_sparse_svd():
388387

389388
# Flexible regex pattern that works across Python versions
390389
sparse_error_pattern = r"(Sparse data|A sparse matrix) was passed, but dense data is required\. Use (?:'.*?'|X\.toarray\(\)) to convert to a dense numpy array\."
391-
with assert_raises_regex(TypeError, sparse_error_pattern):
390+
with pytest.raises(TypeError, match=sparse_error_pattern):
392391
G.inverse_transform(sp.csr_matrix(G.data)[:, 0])
393392

394-
with assert_raises_regex(TypeError, sparse_error_pattern):
393+
with pytest.raises(TypeError, match=sparse_error_pattern):
395394
G.inverse_transform(sp.csr_matrix(G.data)[:, :15])
396395

397396
with assert_raises_message(

test/test_estimator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_estimator():
4242
assert E.graph is None
4343

4444

45-
@parameterized(
45+
@parameterized.expand(
4646
[
4747
("precomputed", 1 - np.eye(10), "distance"),
4848
("precomputed", np.eye(10), "affinity"),

test/test_exact.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
from load_tests import build_graph
66
from load_tests import data
77
from load_tests import graphtools
8-
from load_tests import nose2
98
from load_tests import np
109
from load_tests import PCA
1110
from load_tests import pdist
1211
from load_tests import pygsp
1312
from load_tests import sp
1413
from load_tests import squareform
1514
from load_tests import TruncatedSVD
16-
from nose.tools import assert_warns_regex
15+
16+
import pytest
1717
from scipy.sparse.csgraph import shortest_path
1818

1919
#####################################################
@@ -98,17 +98,17 @@ def test_precomputed_nonzero_diagonal():
9898

9999

100100
def test_duplicate_data():
101-
with assert_warns_regex(
101+
with pytest.warns(
102102
RuntimeWarning,
103-
r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.",
103+
match=r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.",
104104
):
105105
build_graph(np.vstack([data, data[:10]]), n_pca=20, decay=10, thresh=0)
106106

107107

108108
def test_many_duplicate_data():
109-
with assert_warns_regex(
109+
with pytest.warns(
110110
RuntimeWarning,
111-
"Detected zero distance between ([0-9]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.",
111+
match=r"Detected zero distance between ([0-9and,\s]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.",
112112
):
113113
build_graph(np.vstack([data, data]), n_pca=20, decay=10, thresh=0)
114114

test/test_knn.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from load_tests import pygsp
1313
from load_tests import sp
1414
from load_tests import TruncatedSVD
15-
from nose.tools import assert_raises_regex
16-
from nose.tools import assert_warns_regex
15+
16+
import pytest
1717
from scipy.sparse.csgraph import shortest_path
18+
1819
from scipy.spatial.distance import pdist
1920
from scipy.spatial.distance import squareform
2021

@@ -50,17 +51,17 @@ def test_build_knn_with_sample_idx():
5051

5152

5253
def test_duplicate_data():
53-
with assert_warns_regex(
54+
with pytest.warns(
5455
RuntimeWarning,
55-
r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.",
56+
match=r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.",
5657
):
5758
build_graph(np.vstack([data, data[:9]]), n_pca=None, decay=10, thresh=1e-4)
5859

5960

6061
def test_duplicate_data_many():
61-
with assert_warns_regex(
62+
with pytest.warns(
6263
RuntimeWarning,
63-
"Detected zero distance between ([0-9]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.",
64+
match=r"Detected zero distance between ([0-9and,\s]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.",
6465
):
6566
build_graph(np.vstack([data, data[:21]]), n_pca=None, decay=10, thresh=1e-4)
6667

@@ -305,8 +306,9 @@ def test_knnmax():
305306
)
306307
assert isinstance(G2, graphtools.graphs.kNNGraph)
307308
assert G.N == G2.N
308-
assert np.all(G.dw == G2.dw)
309-
assert (G.W - G2.W).nnz == 0
309+
np.testing.assert_allclose(G.dw, G2.dw)
310+
# Use allclose for sparse matrices to handle floating-point precision
311+
np.testing.assert_allclose(G.W.toarray(), G2.W.toarray(), rtol=1e-7, atol=1e-10)
310312
finally:
311313
gg.NUMBA_AVAILABLE = original_numba
312314

test/test_landmark.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from load_tests import digits
88
from load_tests import generate_swiss_roll
99
from load_tests import graphtools
10-
from load_tests import nose2
1110
from load_tests import np
1211

1312
import pygsp

0 commit comments

Comments
 (0)