diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7dc7d83b1..d0f441946 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -38,7 +38,7 @@ jobs: python -m pip install -e .[dmet,ebcc] --user - name: Run unit tests run: | - python -m pip install pytest pytest-cov --user + python -m pip install pytest pytest-cov pytest-xdist pytest-timeout --user python .github/workflows/run_tests.py - name: Upload to codecov uses: codecov/codecov-action@v3 diff --git a/.github/workflows/run_tests.py b/.github/workflows/run_tests.py index ef87a2887..d5ac179ed 100644 --- a/.github/workflows/run_tests.py +++ b/.github/workflows/run_tests.py @@ -8,6 +8,8 @@ args = [ "vayesta/tests", "--cov=vayesta", + "-n=auto", # Multi-processing with pytest-xdist + "-s", # Show stdout output ] if len(sys.argv) > 1 and sys.argv[1] == "--with-veryslow": diff --git a/examples/run_examples.py b/examples/run_examples.py deleted file mode 100644 index 30739db10..000000000 --- a/examples/run_examples.py +++ /dev/null @@ -1,28 +0,0 @@ -import os -import sys -import subprocess - -if len(sys.argv) > 1: - directory = sys.argv[1] -else: - directory = os.path.abspath(os.path.dirname(__file__)) - -examples = os.popen("find . | grep \.py$").readlines() - -assert len(examples) > 0 - -N = len(examples) -errs = [] -for eg in examples[1:]: - print("Running %s" % eg) - errno = subprocess.call("python " + eg[:-1] + " -q", shell=True) - if errno != 0: - print("\033[91mException in %s \033[0m" % eg) - errs.append(eg) - -if len(errs) == 0: - print("\033[92mAll examples passed \033[0m") -else: - print("\033[91m Exceptions found: %d/%d examples failed \033[0m" % (len(errs), len(examples))) - for eg in errs: - print("Execption in %s" % eg) diff --git a/vayesta/tests/test_examples.py b/vayesta/tests/test_examples.py new file mode 100644 index 000000000..2c06cc0cd --- /dev/null +++ b/vayesta/tests/test_examples.py @@ -0,0 +1,32 @@ +import sys +from pathlib import Path +import importlib +from time import perf_counter + +import pytest + + +example_path = Path(__file__).parent.parent.parent / 'examples' +examples_files = list(example_path.glob('**/*.py')) +timings = {} + + +@pytest.fixture(params=examples_files, ids=lambda x: '/'.join([x.parent.name, x.name])) +def example_file(request): + return request.param + + +@pytest.fixture(scope='module', autouse=True) +def report_timings() -> None: + yield + for name, time in timings.items(): + print(f"Time for {name:20s}: {time:.1f} s") + + +@pytest.mark.timeout(300) +def test_example(example_file): + spec = importlib.util.spec_from_file_location(example_file.name, str(example_file)) + example = importlib.util.module_from_spec(spec) + t_start = perf_counter() + spec.loader.exec_module(example) + timings['/'.join([example_file.parent.name, example_file.name])] = perf_counter() - t_start