Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples_test.py cannot be run in parallel #475

Closed
elcorto opened this issue Aug 3, 2023 · 1 comment · Fixed by #517
Closed

examples_test.py cannot be run in parallel #475

elcorto opened this issue Aug 3, 2023 · 1 comment · Fixed by #517

Comments

@elcorto
Copy link
Member

elcorto commented Aug 3, 2023

The order of test execution is given by the order of tests in a test script. In examples_test.py, test_basic_ex01 is run first, creating be_model.zip, which is needed by some other tests in the script.

When test are executed in parallel (e.g. using pytest-xdist), the order is random and some tests that depend on the existence of that artifact will fail.

@elcorto
Copy link
Member Author

elcorto commented Aug 3, 2023

A simple fix that doesn't depend on pytest plugins is:

diff --git a/test/examples_test.py b/test/examples_test.py
index 8fb82eac..531f0018 100644
--- a/test/examples_test.py
+++ b/test/examples_test.py
@@ -1,16 +1,28 @@
 """Test whether the examples are still working."""

 import runpy
+import os

 import pytest


+# There is https://github.com/RKrahl/pytest-dependency and
+# https://github.com/pytest-dev/pytest-order but we like to keep in simple
+# here. When running in parallel (pytest-xdist), we execute ex01 at most twice,
+# which is OK.
+def check_and_create_ex01_artifact():
+    if not os.path.exists("be_model.zip"):
+        print("Running example basic/ex01 in preparation another example")
+        runpy.run_path("../examples/basic/ex01_train_network.py")
+
+
 @pytest.mark.examples
 class TestExamples:
     def test_basic_ex01(self):
         runpy.run_path("../examples/basic/ex01_train_network.py")

     def test_basic_ex02(self):
+        check_and_create_ex01_artifact()
         runpy.run_path("../examples/basic/ex02_test_network.py")

     def test_basic_ex03(self):
@@ -20,9 +32,11 @@ class TestExamples:
         runpy.run_path("../examples/basic/ex04_hyperparameter_optimization.py")

     def test_basic_ex05(self):
+        check_and_create_ex01_artifact()
         runpy.run_path("../examples/basic/ex05_run_predictions.py")

     def test_basic_ex06(self):
+        check_and_create_ex01_artifact()
         runpy.run_path("../examples/basic/ex06_ase_calculator.py")

elcorto added a commit to elcorto/mala that referenced this issue Aug 3, 2023
elcorto added a commit to elcorto/mala that referenced this issue Aug 11, 2023
@pcagas pcagas linked a pull request May 31, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant