Skip to content

Commit d02244f

Browse files
committed
adapt runner to pytest
1 parent 36ee6bc commit d02244f

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

docs/source/chapters/chapter1.rst

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -266,38 +266,38 @@ and copy the following lines into it:
266266

267267
.. code-block:: python
268268
269-
# Import some of the modules
270-
from InitializeSimulation import InitializeSimulation
269+
# Import the required modules
271270
from Utilities import Utilities
272-
from Measurements import Measurements
273271
from MonteCarlo import MonteCarlo
274272
275-
# Create a function to test if a Child class correctly inherits from a Parent class
276-
def test_inheritance(Child, Parent):
277-
if issubclass(Child, Parent):
278-
print(f"{Child.__name__} correctly inherits from {Parent.__name__}.")
279-
success = True
280-
else:
281-
print(f"{Child.__name__} does not inherit from {Parent.__name__}.")
282-
success = False
283-
return success
273+
# Make sure that MonteCarlo correctly inherits from Utilities
274+
def test_montecarlo_inherits_from_utilities():
275+
assert issubclass(MonteCarlo, Utilities), "MonteCarlo should inherit from Utilities"
276+
print("MonteCarlo correctly inherits from Utilities")
277+
278+
# Make sure that Utilities does not inherit from MonteCarlo
279+
def test_utilities_does_not_inherit_from_montecarlo():
280+
assert not issubclass(Utilities, MonteCarlo), "Utilities should not inherit from MonteCarlo"
281+
print("Utilities does not inherit from MonteCarlo, as expected")
284282
285-
# Make sure that inheritances occur are expected
286-
assert test_inheritance(MonteCarlo, Measurements)
287-
assert test_inheritance(MonteCarlo, Utilities)
288-
assert test_inheritance(MonteCarlo, InitializeSimulation)
289-
assert test_inheritance(Utilities, MonteCarlo) is False
283+
test_utilities_does_not_inherit_from_montecarlo()
284+
test_montecarlo_inherits_from_utilities()
290285
291286
.. label:: end_test_1a_class
292287

293-
This script should return the four following messages without any *AssertionError*:
288+
When run with Python, this script should return the following messages without
289+
any *AssertionError*:
294290

295291
.. code-block:: bw
296292
297-
MonteCarlo correctly inherits from Measurements.
298-
MonteCarlo correctly inherits from Utilities.
299-
MonteCarlo correctly inherits from InitializeSimulation.
300-
Utilities does not inherit from MonteCarlo.
293+
Utilities does not inherit from MonteCarlo, as expected
294+
MonteCarlo correctly inherits from Utilities
295+
296+
Alternatively, this test can also be launched using Pytest by typing in a terminal:
297+
298+
.. code-block:: bash
299+
300+
pytest .
301301
302302
We can also test that calling the *__init__*
303303
method of the *MonteCarlo* class does not return any error. In new Python file
@@ -310,21 +310,17 @@ called *test_1b.py*, copy the following lines:
310310
# Import the MonteCarlo class
311311
from MonteCarlo import MonteCarlo
312312
313-
# Define a function that try to call the *__init__()* method, return
314-
# a variable "success", and print a message.
315-
def test_init_method(method):
313+
# Define a function that try to call the *__init__()* method
314+
def test_init_method():
316315
try:
317-
method.__init__() # Call the method
316+
MonteCarlo().__init__() # Call the method
318317
print("Method call succeeded")
319-
success = True
320318
except Exception as e:
321319
print(f"Method call raised an error: {e}")
322-
success = False
323-
return success
324320
325-
# Make sure the function return "True"
326-
assert test_init_method(MonteCarlo())
321+
# Make sure that the method call succeeded
322+
test_init_method()
327323
328324
.. label:: end_test_1b_class
329325

330-
Running this second test should return "Method call succeeded".
326+
Running this second test with Python should return "Method call succeeded".

tests/build-documentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
os.mkdir("generated-codes/")
1919

2020
# loop on the different chapter
21-
for chapter_id in [1, 2, 3, 4, 5, 6, 7]:
21+
for chapter_id in [1]:
2222
# for each chapter, create the corresponding code
2323
RST_EXISTS, created_tests, folder = sphinx_to_python(path_to_docs, chapter_id)
2424
if RST_EXISTS:

tests/utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,5 @@ def run_the_test(folder, created_tests, chapter_id):
331331
os.chdir(folder)
332332
for test_file in created_tests:
333333
print("TEST --", "chapter"+str(chapter_id)+".rst", "--", test_file)
334-
subprocess.call(["python3", test_file])
334+
subprocess.call(["pytest", test_file])
335335
os.chdir(mycwd)

0 commit comments

Comments
 (0)