Skip to content

Commit add8efb

Browse files
authored
Merge pull request #9 from ting-ff/bugfix/conditional-test-fixtures
Bugfix/conditional test fixtures
2 parents fd7f686 + 034a821 commit add8efb

File tree

5 files changed

+82
-19
lines changed

5 files changed

+82
-19
lines changed

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pytest-cov = "*"
1818
pytest-pep8 = "*"
1919
pytest-rerunfailures = "*"
2020
pytest-xdist = "*"
21-
pytest-xprocess = "*"
21+
pytest-xprocess = ">=1.0"
2222
redis = "*"
2323
sphinx = "*"
2424
tox = "*"

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ https://github.com/python/typed_ast/issues/97). Also with Pipenv you can't
143143
have a second Pipfile. This is why for now we don't have `mypy` listed as a dev package
144144
in the Pipfile.
145145

146+
### Testing
147+
148+
The test suite uses pytest and includes fixtures for testing different cache backends. The `caches` fixture in `tests/conftest.py` is parametrized to test various cache types (e.g., 'simple', 'filesystem', 'redis', 'memcached') and eviction strategies (e.g., 'time-based', 'rest-based', 'rest-and-time-based').
149+
150+
The fixture conditionally depends on server fixtures (e.g., `redis_server`, `memcache_server`) based on the cache type being tested. This ensures that pytest only starts the servers that are actually needed for the test, reducing unnecessary overhead and preventing tests from failing due to missing servers if they don't need them.
151+
152+
For example, if you are testing the 'simple' or 'filesystem' cache types, pytest will not attempt to start Redis or Memcached servers, as they are not required for these cache types.
153+
154+
To run the tests, you can use the following commands:
155+
156+
```
157+
# Run all tests
158+
$ pytest
159+
160+
# Run tests for a specific cache type
161+
$ pytest -k "simple"
162+
163+
# Run tests for a specific eviction strategy
164+
$ pytest -k "time-based"
165+
```
166+
167+
For more information on the test fixtures and how they are used, see the `tests/conftest.py` file.
168+
146169
## Credits
147170

148171
As this is a port of the popular [Flask-Caching](https://github.com/sh4nks/flask-caching) library

tests/async_tests/test_app.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,27 @@ async def on_get(self, req, resp):
256256
@pytest.mark.parametrize("eviction_strategy", [
257257
*EVICTION_STRATEGIES
258258
])
259-
def test_caching_content_type_json_only(tmp_path, redis_server, redis_sentinel_server,
260-
memcache_server, async_cache_type, eviction_strategy):
259+
def test_caching_content_type_json_only(tmp_path, async_cache_type, eviction_strategy, request):
261260
""" Testing that the Content-Type header does NOT get cached
262261
when the CACHE_CONTENT_TYPE_JSON_ONLY = True is set, which means
263262
that no msgpack serialization is used in this case, which should mean this
264263
gives a nice performance bump when the Content-Type header caching is
265264
not required because the application/json Content-Type (which is the default
266265
in Falcon) is sufficient for the app.
267266
"""
268-
if async_cache_type == 'redissentinel' and os.getenv('TRAVIS', 'no') == 'yes':
269-
pytest.skip("Unfortunately on Travis Redis Sentinel currently can't be installed")
267+
# Handle cache types that require Redis
268+
if async_cache_type == "redis":
269+
request.getfixturevalue("redis_server")
270+
271+
elif async_cache_type == 'redissentinel':
272+
if os.getenv('TRAVIS', 'no') == 'yes':
273+
pytest.skip("Unfortunately on Travis Redis Sentinel currently can't be installed")
274+
275+
request.getfixturevalue("redis_sentinel_server")
276+
277+
# Handle cache types that require Memcached
278+
elif async_cache_type in ("memcached", "gaememcached"):
279+
request.getfixturevalue("memcache_server")
270280

271281
if True:
272282
cache = AsyncCache(

tests/conftest.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from falcon_caching.cache import SUPPORTED_HASH_FUNCTIONS
1212

1313
try:
14-
__import__("pytest_xprocess")
1514
from xprocess import ProcessStarter
1615
except ImportError:
1716

@@ -95,15 +94,26 @@ def pytest_sessionfinish(session, exitstatus):
9594

9695
# parametrized fixture to create caches with different types (eg backends)
9796
@pytest.fixture(params=CACHE_TYPES)
98-
def caches(request, tmp_path, redis_server, redis_sentinel_server, memcache_server):
97+
def caches(request, tmp_path):
9998
""" Time-based cache parametrized to generate a cache
10099
for each cache_type (eg backend)
101100
"""
102-
if request.param == 'redissentinel' and os.getenv('TRAVIS', 'no') == 'yes':
103-
pytest.skip("Unfortunately on Travis Redis Sentinel currently can't be installed")
101+
# Handle cache types that require Redis
102+
if request.param == "redis":
103+
request.getfixturevalue("redis_server")
104+
elif request.param == "redissentinel":
105+
# check if we are on travis, if so skip this test
106+
if os.getenv("TRAVIS", "no") == "yes":
107+
pytest.skip("Unfortunately on Travis Redis Sentinel currently can't be installed")
108+
109+
request.getfixturevalue("redis_sentinel_server")
110+
111+
# Handle cache types that require Memcached
112+
elif request.param in ("memcached", "gaememcached", "saslmemcached", "spreadsaslmemcached"):
113+
request.getfixturevalue("memcache_server")
104114

105115
# uwsgi tests should only run if running under uwsgi
106-
if request.param == 'uwsgi':
116+
elif request.param == "uwsgi":
107117
try:
108118
import uwsgi
109119
except ImportError:
@@ -129,12 +139,22 @@ def caches(request, tmp_path, redis_server, redis_sentinel_server, memcache_serv
129139

130140
# parametrized fixture to create caches with different types (eg backends)
131141
@pytest.fixture(params=ASYNC_CACHE_TYPES)
132-
def async_caches(request, tmp_path, redis_server, redis_sentinel_server, memcache_server):
133-
""" Time-based cache parametrized to generate a cache
142+
def async_caches(request, tmp_path):
143+
"""Time-based cache parametrized to generate a cache
134144
for each cache_type (eg backend)
135145
"""
136-
if request.param == 'redissentinel' and os.getenv('TRAVIS', 'no') == 'yes':
137-
pytest.skip("Unfortunately on Travis Redis Sentinel currently can't be installed")
146+
# Handle cache types that require Redis
147+
if request.param == "redis":
148+
request.getfixturevalue("redis_server")
149+
elif request.param == "redissentinel":
150+
if os.getenv("TRAVIS", "no") == "yes":
151+
pytest.skip("Unfortunately on Travis Redis Sentinel currently can't be installed")
152+
153+
request.getfixturevalue("redis_sentinel_server")
154+
155+
# Handle cache types that require Memcached
156+
elif request.param in ("memcached", "gaememcached"):
157+
request.getfixturevalue("memcache_server")
138158

139159
# build a dict of caches for each eviction strategy
140160
caches = {

tests/test_app.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,30 @@ def on_get(self, req, resp):
231231
@pytest.mark.parametrize("eviction_strategy", [
232232
*EVICTION_STRATEGIES
233233
])
234-
def test_caching_content_type_json_only(tmp_path, redis_server, redis_sentinel_server,
235-
memcache_server, cache_type, eviction_strategy):
234+
def test_caching_content_type_json_only(tmp_path, cache_type, eviction_strategy, request):
236235
""" Testing that the Content-Type header does NOT get cached
237236
when the CACHE_CONTENT_TYPE_JSON_ONLY = True is set, which means
238237
that no msgpack serialization is used in this case, which should mean this
239238
gives a nice performance bump when the Content-Type header caching is
240239
not required because the application/json Content-Type (which is the default
241240
in Falcon) is sufficient for the app.
242241
"""
243-
if cache_type == 'redissentinel' and os.getenv('TRAVIS', 'no') == 'yes':
244-
pytest.skip("Unfortunately on Travis Redis Sentinel currently can't be installed")
242+
# Handle cache types that require Redis
243+
if cache_type == "redis":
244+
request.getfixturevalue("redis_server")
245+
246+
elif cache_type == 'redissentinel':
247+
if os.getenv('TRAVIS', 'no') == 'yes':
248+
pytest.skip("Unfortunately on Travis Redis Sentinel currently can't be installed")
249+
250+
request.getfixturevalue("redis_sentinel_server")
251+
252+
# Handle cache types that require Memcached
253+
elif cache_type in ("memcached", "gaememcached", "saslmemcached", "spreadsaslmemcached"):
254+
request.getfixturevalue("memcache_server")
245255

246256
# uwsgi tests should only run if running under uwsgi
247-
if cache_type == 'uwsgi':
257+
elif cache_type == 'uwsgi':
248258
try:
249259
import uwsgi
250260
except ImportError:

0 commit comments

Comments
 (0)