Skip to content

Commit 6c4c8de

Browse files
m-strzelczykdandhleebusunkim96parthea
authored
feat: Tests can be run in parallel. (#7550)
* feat: Tests can be run in parallel. * Updating AUTHORING_GUIDE to include info about parallel tests * Update AUTHORING_GUIDE.md Co-authored-by: Dan Lee <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 029e84c commit 6c4c8de

File tree

2 files changed

+49
-34
lines changed

2 files changed

+49
-34
lines changed

AUTHORING_GUIDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ example](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/ap
394394
having them in the test itself.
395395
* Avoid infinite loops.
396396
* Retry RPCs
397+
* You can enable running tests in parallel by adding `pytest-parallel` or `pytest-xdist`
398+
to your `requirements-test.txt` file.
397399
398400
### Arrange, Act, Assert
399401

noxfile-template.py

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -184,42 +184,55 @@ def _session_tests(
184184
) -> None:
185185
# check for presence of tests
186186
test_list = glob.glob("*_test.py") + glob.glob("test_*.py")
187+
test_list.extend(glob.glob("tests"))
188+
187189
if len(test_list) == 0:
188190
print("No tests found, skipping directory.")
189-
else:
190-
if TEST_CONFIG["pip_version_override"]:
191-
pip_version = TEST_CONFIG["pip_version_override"]
192-
session.install(f"pip=={pip_version}")
193-
"""Runs py.test for a particular project."""
194-
if os.path.exists("requirements.txt"):
195-
if os.path.exists("constraints.txt"):
196-
session.install("-r", "requirements.txt", "-c", "constraints.txt")
197-
else:
198-
session.install("-r", "requirements.txt")
199-
200-
if os.path.exists("requirements-test.txt"):
201-
if os.path.exists("constraints-test.txt"):
202-
session.install(
203-
"-r", "requirements-test.txt", "-c", "constraints-test.txt"
204-
)
205-
else:
206-
session.install("-r", "requirements-test.txt")
207-
208-
if INSTALL_LIBRARY_FROM_SOURCE:
209-
session.install("-e", _get_repo_root())
210-
211-
if post_install:
212-
post_install(session)
213-
214-
session.run(
215-
"pytest",
216-
*(PYTEST_COMMON_ARGS + session.posargs),
217-
# Pytest will return 5 when no tests are collected. This can happen
218-
# on travis where slow and flaky tests are excluded.
219-
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
220-
success_codes=[0, 5],
221-
env=get_pytest_env_vars(),
222-
)
191+
return
192+
193+
if TEST_CONFIG["pip_version_override"]:
194+
pip_version = TEST_CONFIG["pip_version_override"]
195+
session.install(f"pip=={pip_version}")
196+
"""Runs py.test for a particular project."""
197+
concurrent_args = []
198+
if os.path.exists("requirements.txt"):
199+
if os.path.exists("constraints.txt"):
200+
session.install("-r", "requirements.txt", "-c", "constraints.txt")
201+
else:
202+
session.install("-r", "requirements.txt")
203+
with open("requirements.txt") as rfile:
204+
packages = rfile.read()
205+
206+
if os.path.exists("requirements-test.txt"):
207+
if os.path.exists("constraints-test.txt"):
208+
session.install(
209+
"-r", "requirements-test.txt", "-c", "constraints-test.txt"
210+
)
211+
else:
212+
session.install("-r", "requirements-test.txt")
213+
with open("requirements-test.txt") as rtfile:
214+
packages += rtfile.read()
215+
216+
if INSTALL_LIBRARY_FROM_SOURCE:
217+
session.install("-e", _get_repo_root())
218+
219+
if post_install:
220+
post_install(session)
221+
222+
if "pytest-parallel" in packages:
223+
concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto'])
224+
elif "pytest-xdist" in packages:
225+
concurrent_args.extend(['-n', 'auto'])
226+
227+
session.run(
228+
"pytest",
229+
*(PYTEST_COMMON_ARGS + session.posargs + concurrent_args),
230+
# Pytest will return 5 when no tests are collected. This can happen
231+
# on travis where slow and flaky tests are excluded.
232+
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
233+
success_codes=[0, 5],
234+
env=get_pytest_env_vars(),
235+
)
223236

224237

225238
@nox.session(python=ALL_VERSIONS)

0 commit comments

Comments
 (0)