Skip to content

Commit 2f9b66b

Browse files
committed
Make test caching opt-in with --cached (#1464)
* Make test caching opt-in `--cached` * Remove unused param
1 parent c26cbe9 commit 2f9b66b

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

test/conftest.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,8 @@
1111
1212
**Documenting Tests**
1313
To ease in understanding of tests, what is being tested and what's expected of the test,
14-
each test should be documented with what it's parameters/fixtures are as well as what
15-
the test expects to happen, regardless of the tests implemntation.
16-
17-
Parameters
18-
----------
19-
param1: Type
20-
...
21-
22-
param2: Type
23-
...
24-
25-
Fixtures
26-
--------
27-
make_something: Callable[..., Something]
28-
Factory to make Something
14+
each test should doc expected bhaviour, not describe the steps of the test.
15+
Commenst relating to how a test does things can be left in the tests and not the doc.
2916
3017
Expects
3118
-------
@@ -53,7 +40,7 @@
5340
tests and between different test runs. This is primarly used with `cases` so that tests
5441
requiring the same kind of expensive case and used cached values.
5542
56-
Use `pytest --cache-clear` to clear the cahce
43+
Use `pytest --cached` to use this feature.
5744
5845
See `test/test_automl/cases.py` for example of how the fixtures from
5946
`test/fixtures/caching.py` can be used to cache objects between tests.
@@ -87,6 +74,7 @@ def test_something_does_x(arg1, make_something):
8774
from typing import Any, Iterator, List, Optional
8875

8976
import re
77+
import shutil
9078
import signal
9179
from pathlib import Path
9280

@@ -99,7 +87,7 @@ def test_something_does_x(arg1, make_something):
9987

10088

10189
HERE = Path(__file__)
102-
AUTOSKLEARN_CACHE_NAME = "autosklearn"
90+
AUTOSKLEARN_CACHE_NAME = "autosklearn-cache"
10391

10492

10593
def walk(path: Path, include: Optional[str] = None) -> Iterator[Path]:
@@ -162,6 +150,18 @@ def pytest_sessionstart(session: Session) -> None:
162150
session : Session
163151
The pytest session object
164152
"""
153+
config = session.config
154+
cache = config.cache
155+
156+
if cache is None:
157+
return
158+
159+
# We specifically only remove the cached items dir, not any information
160+
# about previous tests which also exist in `.pytest_cache`
161+
if not config.getoption("--cached"):
162+
dir = cache.mkdir(AUTOSKLEARN_CACHE_NAME)
163+
shutil.rmtree(dir)
164+
165165
return
166166

167167

@@ -202,7 +202,6 @@ def pytest_collection_modifyitems(
202202
def pytest_configure(config: Config) -> None:
203203
"""Used to register marks"""
204204
config.addinivalue_line("markers", "todo: Mark test as todo")
205-
config.addinivalue_line("markers", "slow: Mark test as slow")
206205

207206

208207
pytest_plugins = fixture_modules()
@@ -225,3 +224,9 @@ def pytest_addoption(parser: Parser) -> None:
225224
default=False,
226225
help="Disable tests marked as slow",
227226
)
227+
parser.addoption(
228+
"--cached",
229+
action="store_true",
230+
default=False,
231+
help="Cache everything between invocations of pytest",
232+
)

test/fixtures/caching.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from pytest import FixtureRequest
1414
from pytest_cases import fixture
1515

16+
from test.conftest import AUTOSKLEARN_CACHE_NAME
17+
1618

1719
class Cache:
1820
"""Used for the below fixtures.
@@ -143,7 +145,7 @@ def cache(request: FixtureRequest) -> Callable[[str], Cache]:
143145
pytest_cache = request.config.cache
144146
assert pytest_cache is not None
145147

146-
cache_dir = pytest_cache.mkdir("autosklearn-cache")
148+
cache_dir = pytest_cache.mkdir(AUTOSKLEARN_CACHE_NAME)
147149
return partial(Cache, cache_dir=cache_dir)
148150

149151

@@ -153,6 +155,6 @@ def automl_cache(request: FixtureRequest) -> Callable[[str], AutoMLCache]:
153155
pytest_cache = request.config.cache
154156
assert pytest_cache is not None
155157

156-
cache_dir = pytest_cache.mkdir("autosklearn-cache")
158+
cache_dir = pytest_cache.mkdir(AUTOSKLEARN_CACHE_NAME)
157159
verbosity = request.config.getoption("verbose")
158160
return partial(AutoMLCache, cache_dir=cache_dir, verbose=verbosity)

0 commit comments

Comments
 (0)