Skip to content

Commit 67c4743

Browse files
germa89Copilotpyansys-ci-bot
authored
refactor: remove RUNNING_TESTS global flag and running_test fixture (#4455)
* refactor: remove RUNNING_TESTS global flag and running_test fixture Replace test-specific production code with mock/patch in tests. - Remove RUNNING_TESTS: bool = False global from �nsys.mapdl.core - Remove the if pymapdl.RUNNING_TESTS early-return guard from _retrieve_file() in downloads.py and drop the dead _test parameters from _retrieve_file and _download_file - Remove the RUNNING_TESTS import and dead log block from un_every_import() in helpers.py - Simplify the session-ID check condition in mapdl_core.py: pymapdl.RUNNING_TESTS or self._strict_session_id_check → self._strict_session_id_check - Remove the Running_test context-manager class, the unning_test fixture, and the pymapdl.RUNNING_TESTS = True module-level statement from ests/conftest.py - In est_examples.py, replace with running_test(): (seven tests) with patch('...downloads._retrieve_file', return_value=True); strip the now-unnecessary with running_test(False): wrappers from est_bracket and est_failed_download - In est_mapdl.py, remove the Running_test import, drop the with running_test(): wrapper in est_session_id (covered by the pre-existing _strict_session_id_check = True), and drop the with Running_test(False): wrapper in est_igesin_whitespace Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * chore: adding changelog file 4455.miscellaneous.md [dependabot-skip] * fix: matching test return with function return type --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
1 parent 88e6efc commit 67c4743

8 files changed

Lines changed: 53 additions & 69 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove RUNNING_TESTS global flag and running_test fixture

src/ansys/mapdl/core/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
}
6161

6262
BUILDING_GALLERY: bool = False
63-
RUNNING_TESTS: bool = False
6463

6564
DEPRECATING_MINIMUM_PYTHON_VERSION: bool = False
6665
MINIMUM_PYTHON_VERSION: Tuple[int, int] = (3, 10)

src/ansys/mapdl/core/examples/downloads.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,7 @@ def _check_url_exist(url: str) -> bool:
8585

8686

8787
@check_directory_exist(pymapdl.EXAMPLES_PATH)
88-
def _retrieve_file(url: str, filename: str, _test: bool = False) -> str:
89-
# escape test
90-
if pymapdl.RUNNING_TESTS:
91-
return _check_url_exist(url)
92-
88+
def _retrieve_file(url: str, filename: str) -> str:
9389
# First check if file has already been downloaded
9490
local_path = os.path.join(pymapdl.EXAMPLES_PATH, os.path.basename(filename))
9591
local_path_no_zip = local_path.replace(".zip", "")
@@ -109,12 +105,10 @@ def _retrieve_file(url: str, filename: str, _test: bool = False) -> str:
109105
return local_path
110106

111107

112-
def _download_file(
113-
filename: str, directory: Optional[str] = None, _test: Optional[bool] = False
114-
) -> str:
108+
def _download_file(filename: str, directory: Optional[str] = None) -> str:
115109
url = _get_file_url(filename, directory)
116110
try:
117-
return _retrieve_file(url, filename, _test)
111+
return _retrieve_file(url, filename)
118112
except requests.exceptions.HTTPError as e:
119113
raise requests.exceptions.HTTPError(
120114
"Retrieving the file from internet failed.\n"

src/ansys/mapdl/core/helpers.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,4 @@ def run_first_time() -> None:
9494

9595
def run_every_import() -> None:
9696
# Run every time we import PyMAPDL
97-
from ansys.mapdl.core import RUNNING_TESTS
98-
99-
# In case we want to do something specific for testing.
100-
if RUNNING_TESTS: # pragma: no cover
101-
LOG.debug("Running tests on Pytest")
97+
pass

src/ansys/mapdl/core/mapdl_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3408,7 +3408,7 @@ def _check_session_id(self):
34083408

34093409
if pymapdl_session_id is None or self._mapdl_session_id is None:
34103410
return
3411-
elif pymapdl.RUNNING_TESTS or self._strict_session_id_check:
3411+
elif self._strict_session_id_check:
34123412
if pymapdl_session_id != self._mapdl_session_id:
34133413
self._log.error("The session ids do not match")
34143414

tests/conftest.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ def requires_dependency(dependency: str):
214214
_apply_default_theme()
215215

216216

217-
import ansys.mapdl.core as pymapdl
218-
219-
pymapdl.RUNNING_TESTS = True
220-
221217
from ansys.mapdl.core import Mapdl
222218
from ansys.mapdl.core.errors import MapdlExitedError, MapdlRuntimeError
223219
from ansys.mapdl.core.examples import vmfiles
@@ -528,17 +524,6 @@ def wrapped_verify_image_cache(verify_image_cache, pytestconfig):
528524
return verify_image_cache
529525

530526

531-
class Running_test:
532-
def __init__(self, active: bool = True) -> None:
533-
self._state = active
534-
535-
def __enter__(self) -> None:
536-
pymapdl.RUNNING_TESTS = self._state
537-
538-
def __exit__(self, *args) -> None:
539-
pymapdl.RUNNING_TESTS = not self._state
540-
541-
542527
class NullContext:
543528
def __enter__(self):
544529
pass
@@ -550,11 +535,6 @@ def __init__(self):
550535
pass
551536

552537

553-
@pytest.fixture(scope="function")
554-
def running_test():
555-
return Running_test
556-
557-
558538
@pytest.fixture(autouse=True, scope="function")
559539
def run_before_and_after_tests(
560540
request: pytest.FixtureRequest, mapdl: Mapdl

tests/test_examples.py

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import os
2424
import re
2525
from subprocess import PIPE, STDOUT, Popen
26+
from unittest.mock import patch
2627

2728
import pytest
2829

@@ -42,6 +43,8 @@
4243
)
4344
from conftest import requires
4445

46+
DUMMY_PATH = "/dummy/download/path"
47+
4548

4649
def test_check_directory_exist(tmpdir):
4750
tmp_dir = os.path.join(tmpdir, "mytempdir")
@@ -90,10 +93,9 @@ def test_load_verif():
9093

9194

9295
@requires("requests")
93-
def test_bracket(mapdl, cleared, running_test):
96+
def test_bracket(mapdl, cleared):
9497
# note that this method just returns a file path
95-
with running_test(False): # To force downloading the file
96-
bracket_file = examples.download_bracket()
98+
bracket_file = examples.download_bracket()
9799

98100
assert os.path.isfile(bracket_file)
99101

@@ -111,57 +113,72 @@ def test_download_example_data_true_download():
111113

112114

113115
@requires("requests")
114-
def test_failed_download(running_test):
116+
def test_failed_download():
115117
from requests.exceptions import HTTPError
116118

117119
filename = "non_existing_file"
118120
with pytest.raises(HTTPError):
119-
with running_test(active=False): # To force downloading the file
120-
_download_file(filename, directory=None)
121+
_download_file(filename, directory=None)
121122

122123

123124
@requires("requests")
124-
def test_download_cfx_mapping_example_data(running_test):
125-
with running_test():
126-
assert all(download_cfx_mapping_example_data().values())
125+
def test_download_cfx_mapping_example_data():
126+
with patch(
127+
"ansys.mapdl.core.examples.downloads._retrieve_file", return_value=DUMMY_PATH
128+
):
129+
result = download_cfx_mapping_example_data()
130+
assert all(v == DUMMY_PATH for v in result.values())
127131

128132

129133
@requires("requests")
130-
def test_download_manifold_example_data(running_test):
131-
with running_test():
132-
assert all(download_manifold_example_data().values())
134+
def test_download_manifold_example_data():
135+
with patch(
136+
"ansys.mapdl.core.examples.downloads._retrieve_file", return_value=DUMMY_PATH
137+
):
138+
result = download_manifold_example_data()
139+
assert all(v == DUMMY_PATH for v in result.values())
133140

134141

135142
@requires("requests")
136-
def test_download_bracket(running_test):
137-
with running_test():
138-
assert download_bracket() is True
143+
def test_download_bracket():
144+
with patch(
145+
"ansys.mapdl.core.examples.downloads._retrieve_file", return_value=DUMMY_PATH
146+
):
147+
assert download_bracket() == DUMMY_PATH
139148

140149

141150
@requires("requests")
142-
def test_download_vtk_rotor(running_test):
143-
with running_test():
144-
assert download_vtk_rotor() is True
151+
def test_download_vtk_rotor():
152+
with patch(
153+
"ansys.mapdl.core.examples.downloads._retrieve_file", return_value=DUMMY_PATH
154+
):
155+
assert download_vtk_rotor() == DUMMY_PATH
145156

146157

147158
@requires("requests")
148-
def test__download_rotor_tech_demo_vtk(running_test):
149-
with running_test():
150-
assert _download_rotor_tech_demo_vtk() is True
159+
def test__download_rotor_tech_demo_vtk():
160+
with patch(
161+
"ansys.mapdl.core.examples.downloads._retrieve_file", return_value=DUMMY_PATH
162+
):
163+
assert _download_rotor_tech_demo_vtk() == DUMMY_PATH
151164

152165

153166
@requires("requests")
154-
def test_download_example_data(running_test):
155-
with running_test():
156-
assert download_example_data("LatheCutter.anf", "geometry") is True
167+
def test_download_example_data():
168+
with patch(
169+
"ansys.mapdl.core.examples.downloads._retrieve_file", return_value=DUMMY_PATH
170+
):
171+
assert download_example_data("LatheCutter.anf", "geometry") == DUMMY_PATH
157172

158173

159174
@requires("requests")
160-
def test_download_tech_demo_data(running_test):
161-
with running_test():
175+
def test_download_tech_demo_data():
176+
with patch(
177+
"ansys.mapdl.core.examples.downloads._retrieve_file", return_value=DUMMY_PATH
178+
):
162179
assert (
163180
download_tech_demo_data("td-21", "ring_stiffened_cylinder_mesh_file.cdb")
164-
is True
181+
== DUMMY_PATH
165182
)
166183

167184

tests/test_mapdl.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
TEST_DPF_BACKEND,
5050
VALID_PORTS,
5151
NullContext,
52-
Running_test,
5352
has_dependency,
5453
requires,
5554
)
@@ -2079,7 +2078,7 @@ def test_get_not_muted(mapdl, cleared):
20792078
mock_not_muted.assert_not_called()
20802079

20812080

2082-
def test_session_id(mapdl, running_test):
2081+
def test_session_id(mapdl):
20832082
# Calling here to make sure we have a session id before testing it.
20842083
mapdl.clear()
20852084

@@ -2098,8 +2097,7 @@ def test_session_id(mapdl, running_test):
20982097

20992098
# Checking real case
21002099
mapdl._session_id_ = copy_
2101-
with running_test():
2102-
assert isinstance(mapdl._check_session_id(), bool)
2100+
assert isinstance(mapdl._check_session_id(), bool)
21032101

21042102
id_ = "123412341234"
21052103
mapdl._session_id_ = id_
@@ -2124,8 +2122,7 @@ def test_check_empty_session_id(mapdl, cleared):
21242122
@requires("requests") # Requires 'requests' package
21252123
def test_igesin_whitespace(mapdl, cleared, tmpdir):
21262124
# make sure we download the IGES file
2127-
with Running_test(False): # allow access to internet
2128-
bracket_file = pymapdl.examples.download_bracket()
2125+
bracket_file = pymapdl.examples.download_bracket()
21292126

21302127
assert os.path.isfile(bracket_file)
21312128

0 commit comments

Comments
 (0)