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

Run tests in a parallel matrix #1031

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .test_durations

Large diffs are not rendered by default.

90 changes: 87 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ uninstall:
@$(call help,$@:)
@pip freeze | grep -v "^-e" | sed "s/@.*//" | xargs pip uninstall -y

## Delete builds and compiled python files.
## Delete builds, cached and compiled python files.
clean: \
$(shell ls -d * | grep "build\|dist") \
$(shell find . -name "*.pyc")
$(shell ls -a | grep "build\|dist\|mypy_\|pytest_") \
$(shell find . -path ./.nox -prune -o -name "*.pyc")
@$(call help,$@:)
@rm -rf $?

Expand Down Expand Up @@ -56,6 +56,90 @@ test: clean check-syntax-errors check-style check-types
@$(call help,$@:)
@env PYTEST_ADDOPTS="${PYTEST_ADDOPTS} --cov=openfisca_core" pytest

## Run openfisca-core tests over a matrix.
test.matrix:
@$(call doc,$@:)
@[ -z $$(pip freeze | grep ^nox) ] \
&& pip install --upgrade nox \
&& ${MAKE} test.matrix \
|| time { \
${MAKE} test.matrix.install --jobs $$(($$(nproc) * 3 / 2 + 1)) ; \
${MAKE} test.matrix.test --jobs $$(($$(nproc) / 2 + 1)) ; \
}

test.matrix.%: $(shell git ls-files "tests/**/*.py")
@args=($(subst -, ,$*)) \
&& [ $${args[3]} -eq 0 ] \
&& echo "[⚙] Installing python $${args[1]} and numpy $${args[2]}…" \
|| echo "[⚙] Testing against python $${args[1]} and numpy $${args[2]} (group $${args[3]}/4)…" \
&& nox -s "$${args[0]}-$${args[1]}($${args[2]})" -- $${args[3]} $? &> /dev/null ;

test.matrix.install: \
test.matrix.test-3.7.11-1.18.5-0 \
test.matrix.test-3.7.11-1.19.5-0 \
test.matrix.test-3.7.11-1.20.3-0 \
test.matrix.test-3.7.11-1.21.2-0 \
test.matrix.test-3.8.12-1.18.5-0 \
test.matrix.test-3.8.12-1.19.5-0 \
test.matrix.test-3.8.12-1.20.3-0 \
test.matrix.test-3.8.12-1.21.2-0 \
test.matrix.test-3.9.7-1.18.5-0 \
test.matrix.test-3.9.7-1.19.5-0 \
test.matrix.test-3.9.7-1.20.3-0 \
test.matrix.test-3.9.7-1.21.2-0 \
;

test.matrix.test: \
test.matrix.test-3.7.11-1.18.5-1 \
test.matrix.test-3.7.11-1.18.5-2 \
test.matrix.test-3.7.11-1.18.5-3 \
test.matrix.test-3.7.11-1.18.5-4 \
test.matrix.test-3.7.11-1.19.5-1 \
test.matrix.test-3.7.11-1.19.5-2 \
test.matrix.test-3.7.11-1.19.5-3 \
test.matrix.test-3.7.11-1.19.5-4 \
test.matrix.test-3.7.11-1.20.3-1 \
test.matrix.test-3.7.11-1.20.3-2 \
test.matrix.test-3.7.11-1.20.3-3 \
test.matrix.test-3.7.11-1.20.3-4 \
test.matrix.test-3.7.11-1.21.2-1 \
test.matrix.test-3.7.11-1.21.2-2 \
test.matrix.test-3.7.11-1.21.2-3 \
test.matrix.test-3.7.11-1.21.2-4 \
test.matrix.test-3.8.12-1.18.5-1 \
test.matrix.test-3.8.12-1.18.5-2 \
test.matrix.test-3.8.12-1.18.5-3 \
test.matrix.test-3.8.12-1.18.5-4 \
test.matrix.test-3.8.12-1.19.5-1 \
test.matrix.test-3.8.12-1.19.5-2 \
test.matrix.test-3.8.12-1.19.5-3 \
test.matrix.test-3.8.12-1.19.5-4 \
test.matrix.test-3.8.12-1.20.3-1 \
test.matrix.test-3.8.12-1.20.3-2 \
test.matrix.test-3.8.12-1.20.3-3 \
test.matrix.test-3.8.12-1.20.3-4 \
test.matrix.test-3.8.12-1.21.2-1 \
test.matrix.test-3.8.12-1.21.2-2 \
test.matrix.test-3.8.12-1.21.2-3 \
test.matrix.test-3.8.12-1.21.2-4 \
test.matrix.test-3.9.7-1.18.5-1 \
test.matrix.test-3.9.7-1.18.5-2 \
test.matrix.test-3.9.7-1.18.5-3 \
test.matrix.test-3.9.7-1.18.5-4 \
test.matrix.test-3.9.7-1.19.5-1 \
test.matrix.test-3.9.7-1.19.5-2 \
test.matrix.test-3.9.7-1.19.5-3 \
test.matrix.test-3.9.7-1.19.5-4 \
test.matrix.test-3.9.7-1.20.3-1 \
test.matrix.test-3.9.7-1.20.3-2 \
test.matrix.test-3.9.7-1.20.3-3 \
test.matrix.test-3.9.7-1.20.3-4 \
test.matrix.test-3.9.7-1.21.2-1 \
test.matrix.test-3.9.7-1.21.2-2 \
test.matrix.test-3.9.7-1.21.2-3 \
test.matrix.test-3.9.7-1.21.2-4 \
;

## Check that the current changes do not break the doc.
test-doc:
@## Usage:
Expand Down
19 changes: 19 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import nox

nox.options.reuse_existing_virtualenvs = True
py_versions = ["3.7.11", "3.8.12", "3.9.7"]
np_versions = ["1.18.5", "1.19.5", "1.20.3", "1.21.2"]


@nox.session(python = py_versions)
@nox.parametrize("numpy", np_versions, ids = np_versions)
def test(session, numpy):
if not int(session.posargs[0]):
session.install("-e", ".[dev]")
session.install("invoke")
session.install("pytest-split")
session.install("pytest-parallel-39")
session.install(f"numpy=={numpy}")

else:
session.run("inv", "test", session.posargs[0], " ".join(session.posargs[1:]))
5 changes: 4 additions & 1 deletion openfisca_core/tools/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ def run_tests(tax_benefit_system, paths, options = None):

"""

argv = ["--capture", "no"]
argv = []

if options.get('pdb'):
argv.append('--pdb')

if options.get('verbose'):
argv.append('--verbose')

if isinstance(paths, str):
paths = [paths]

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
general_requirements = [
'dpath >= 1.5.0, < 2.0.0',
'pytest >= 4.4.1, < 6.0.0', # For openfisca test
'numpy >= 1.11, < 1.21',
'numpy >= 1.18, < 1.22',
'psutil >= 5.4.7, < 6.0.0',
'PyYAML >= 3.10',
'sortedcontainers == 2.2.2',
Expand Down
10 changes: 10 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os

import invoke


@invoke.task
def test(context, group, pattern):
jobs = os.cpu_count() // 4
args = f"PYTEST_ADDOPTS='-qx --workers {jobs} --splits {jobs * 2} --group {group}'"
context.run(f"{args} openfisca test {pattern}")
1 change: 1 addition & 0 deletions tests/core/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_failing_shell_script():
subprocess.check_call(command, stdout = devnull, stderr = devnull)


@pytest.mark.skip(reason = "Subprocesses fail with parallelisation.")
def test_shell_script_with_reform():
yaml_path = os.path.join(yaml_tests_dir, 'test_with_reform_2.yaml')
command = ['openfisca', 'test', yaml_path, '-c', 'openfisca_country_template', '-r', 'openfisca_country_template.reforms.removal_basic_income.removal_basic_income']
Expand Down