Skip to content

Commit 0dabe35

Browse files
authored
Add pydatastructs.test API (#535)
1 parent 119653a commit 0dabe35

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ jobs:
9393
9494
- name: Run tests
9595
run: |
96-
python -m pytest --doctest-modules --cov=./ --cov-report=xml -s
96+
python -c "import pydatastructs; pydatastructs.test(include_benchmarks=True)"
9797
9898
- name: Build Documentation
9999
run: |
@@ -134,15 +134,15 @@ jobs:
134134
135135
- name: Run tests
136136
run: |
137-
python -m pytest --doctest-modules --cov=./ --cov-report=xml -s
137+
python -c "import pydatastructs; pydatastructs.test()"
138138
139139
- name: Build Documentation
140140
run: |
141141
sphinx-build -b html docs/source/ docs/build/html
142142
143143
test-windows:
144144
runs-on: ${{matrix.os}}
145-
timeout-minutes: 10
145+
timeout-minutes: 15
146146
strategy:
147147
fail-fast: false
148148
matrix:
@@ -182,7 +182,7 @@ jobs:
182182
183183
- name: Run tests
184184
run: |
185-
python -m pytest --doctest-modules --cov=./ --cov-report=xml -s
185+
python -c "import pydatastructs; pydatastructs.test()"
186186
187187
- name: Build Documentation
188188
run: |

pydatastructs/graphs/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
__all__.extend(graph.__all__)
88

99
from . import algorithms
10+
from . import adjacency_list
11+
from . import adjacency_matrix
12+
1013
from .algorithms import (
1114
breadth_first_search,
1215
breadth_first_search_parallel,

pydatastructs/utils/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
__all__ = []
22

33
from . import misc_util
4+
from . import testing_util
45
from .misc_util import (
56
TreeNode,
67
MAryTreeNode,
@@ -19,4 +20,7 @@
1920
minimum,
2021
Backend
2122
)
23+
from .testing_util import test
24+
2225
__all__.extend(misc_util.__all__)
26+
__all__.extend(testing_util.__all__)

pydatastructs/utils/testing_util.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
try:
2+
import pytest
3+
except ImportError:
4+
raise Exception("pytest must be installed. Use `pip install pytest` "
5+
"to install it.")
6+
7+
import os
8+
import pathlib
9+
import glob
10+
import types
11+
12+
__all__ = ['test']
13+
14+
15+
# Root pydatastructs directory
16+
ROOT_DIR = pathlib.Path(os.path.abspath(__file__)).parents[2]
17+
18+
19+
SKIP_FILES = ['testing_util.py']
20+
21+
def test(submodules=None, include_benchmarks=False,
22+
benchmarks_size=1000, **kwargs):
23+
"""
24+
Runs the library tests using pytest
25+
26+
Parameters
27+
==========
28+
29+
submodules: Optional, list[str]
30+
List of submodules test to run. By default runs
31+
all the tests
32+
"""
33+
# set benchmarks size
34+
os.environ["PYDATASTRUCTS_BENCHMARK_SIZE"] = str(benchmarks_size)
35+
test_files = []
36+
if submodules:
37+
if not isinstance(submodules, (list, tuple)):
38+
submodules = [submodules]
39+
for path in glob.glob(f'{ROOT_DIR}/**/test_*.py', recursive=True):
40+
skip_test = False
41+
for skip in SKIP_FILES:
42+
if skip in path:
43+
skip_test = True
44+
break
45+
if skip_test:
46+
continue
47+
for sub_var in submodules:
48+
if isinstance(sub_var, types.ModuleType):
49+
sub = sub_var.__name__.split('.')[-1]
50+
elif isinstance(sub_var, str):
51+
sub = sub_var
52+
else:
53+
raise Exception("Submodule should be of type: str or module")
54+
if sub in path:
55+
if not include_benchmarks:
56+
if not 'benchmarks' in path:
57+
test_files.append(path)
58+
else:
59+
test_files.append(path)
60+
break
61+
else:
62+
for path in glob.glob(f'{ROOT_DIR}/**/test_*.py', recursive=True):
63+
skip_test = False
64+
for skip in SKIP_FILES:
65+
if skip in path:
66+
skip_test = True
67+
break
68+
if skip_test:
69+
continue
70+
if not include_benchmarks:
71+
if not 'benchmarks' in path:
72+
test_files.append(path)
73+
else:
74+
test_files.append(path)
75+
76+
extra_args = []
77+
if not kwargs.get("n", False) is False:
78+
extra_args.append("-n")
79+
extra_args.append(str(kwargs["n"]))
80+
81+
pytest.main(extra_args + test_files)

0 commit comments

Comments
 (0)