Skip to content

Commit 5bef0c0

Browse files
authored
[RFC] Default pytest run mode to unit tests only (pytorch#573)
1 parent b65426d commit 5bef0c0

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

.github/workflows/unit_test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ jobs:
3636
python -m pip install torch
3737
python -m pip install -e ".[dev]"
3838
- name: Run unit tests with coverage
39-
run: pytest tests --without-integration --without-slow-integration --cov=. --cov-report=xml --durations=20 -vv
39+
run: pytest tests --cov=. --cov-report=xml --durations=20 -vv
4040
- name: Upload Coverage to Codecov
4141
uses: codecov/codecov-action@v3

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ check-return-types = 'False'
1010
exclude = 'tests/torchtune/models/llama2/scripts/'
1111

1212
[tool.pytest.ini_options]
13-
addopts = ["--showlocals", "--import-mode=importlib"]
13+
addopts = ["--showlocals", "--import-mode=importlib", "--without-integration", "--without-slow-integration"]
1414
# --showlocals will show local variables in tracebacks
1515
# --import-model=importlib ensures pytest doesn't append the repo root to sys.path,
1616
# causing our tests to bypass legitimate import errors
17+
# --without-integration and --without-slow-integration: default to running unit tests only

tests/conftest.py

+28-12
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,39 @@
2121
def pytest_configure(config):
2222
"""
2323
This hook runs before each pytest invocation. Its purpose is to handle optional fetching
24-
of remote artifacts needed for the test run. For testing, you should run one of the following:
24+
of remote artifacts needed for the test run and filtering across unit tests, recipe tests, and
25+
regression tests.
2526
26-
- `pytest tests --without-integration --without-slow-integration`: run unit tests only
27-
- `pytest tests --without-slow-integration`: run unit tests and recipe tests
28-
- `pytest tests`: run all tests
27+
When testing, you should run one of the following:
28+
29+
- `pytest tests`: run unit tests only
30+
- `pytest tests --with-integration`: run unit tests and recipe tests
31+
- `pytest tests --with-integration --with-slow-integration`: run all tests
2932
- `pytest tests -m integration_test`: run recipe tests only
3033
- `pytest tests -m slow_integration_test`: run regression tests only
3134
32-
This hook ensures that the appropriate artifacts are available locally for each of these cases.
33-
It also supports optional silencing of S3 progress bars to reduce CI log spew.
35+
Similar commands apply for filtering in subdirectories or individual test files.
36+
37+
This hook also ensures that the appropriate artifacts are available locally for all of the above cases.
38+
Note that artifact download is determined by the CLI flags, so if you run e.g.
39+
`pytest tests/torchtune/some_unit_test.py -m integration_test`, the integration test
40+
artifacts will be downloaded even if your test doesn't require them.
41+
42+
The hook also supports optional silencing of S3 progress bars to reduce CI log spew via `--silence-s3-logs`.
3443
"""
44+
45+
# To make it more convenient to run an individual unit test, we override the default
46+
# behavior of pytest-integration to run with --without-integration --without-slow-integration
47+
# This means that we need to manually override the values of run_integration and run_slow_integration
48+
# whenever either set of tests is passed via the -m option.
49+
50+
if config.option.markexpr == "integration_test":
51+
config.option.run_integration = True
52+
run_regression_tests = False
53+
if config.option.markexpr == "slow_integration_test":
54+
config.option.run_slow_integration = True
55+
run_recipe_tests = False
56+
3557
# Default is to run both integration and slow integration tests (i.e. both are None)
3658
run_recipe_tests = (
3759
config.option.run_integration is None or config.option.run_integration is True
@@ -41,12 +63,6 @@ def pytest_configure(config):
4163
or config.option.run_slow_integration is True
4264
)
4365

44-
# For -m flags, we run only those tests and so disable the others here
45-
if config.option.markexpr == "integration_test":
46-
run_regression_tests = False
47-
if config.option.markexpr == "slow_integration_test":
48-
run_recipe_tests = False
49-
5066
cmd = str(CACHE_ARTIFACTS_SCRIPT_PATH)
5167

5268
if run_recipe_tests:

0 commit comments

Comments
 (0)