Testing: exceptions and caches
Briefly

"Kacper Borucki blogged about parameterizing exception testing, and linked to pytest docs and a StackOverflow answer with similar approaches. The common way to test exceptions is to use pytest.raises as a context manager, and have separate tests for the cases that succeed and those that fail. Instead, this approach lets you unify them. I tweaked it to this, which I think reads nicely: One parameterized test that covers both good and bad outcomes. Nice."
"The @functools.lru_cache decorator (and its convenience cousin@cache) are good ways to save the result of a function so that you don't have to compute it repeatedly. But, they hide an implicit global in your program: the dictionary of cached results. This can interfere with testing. Your tests should all be isolated from each other. You don't want a side effect of one test to affect the outcome of another test. The hidden global dictionary will do just that. The first test calls the cached function, then the second test gets the cached value, not a newly computed one."
Parameterize exception tests so a single test can cover both successful and failing outcomes, using pytest.raises where appropriate. The functools.lru_cache (and @cache) store results in an implicit global dictionary, which can break test isolation by letting one test’s call affect another. lru_cache works safely on pure functions where results depend only on arguments; problems arise when cached functions read environment or external APIs and tests mock those behaviors. The lru_cache decorator exposes a cache-clear method that can be called to remove cached entries, though forgetting to clear new cached functions can cause test failures.
Read at Nedbatchelder
Unable to calculate read time
[
|
]