Changeset View
Changeset View
Standalone View
Standalone View
swh/core/pytest_plugin.py
Show First 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | def get_response_cb(request, context, datadir, | ||||
context.headers['content-length'] = str(path.getsize(filepath)) | context.headers['content-length'] = str(path.getsize(filepath)) | ||||
return fd | return fd | ||||
@pytest.fixture | @pytest.fixture | ||||
def datadir(request): | def datadir(request): | ||||
"""By default, returns the test directory's data directory. | """By default, returns the test directory's data directory. | ||||
This can be overriden on a per arborescence basis. Add an override | This can be overridden on a per arborescence basis. Add an override | ||||
definition in the local conftest, for example: | definition in the local conftest, for example: | ||||
import pytest | import pytest | ||||
from os import path | from os import path | ||||
@pytest.fixture | @pytest.fixture | ||||
def datadir(): | def datadir(): | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | |||||
requests_mock_datadir = requests_mock_datadir_factory([]) | requests_mock_datadir = requests_mock_datadir_factory([]) | ||||
# Implementation for multiple visits behavior: | # Implementation for multiple visits behavior: | ||||
# - first time, it checks for a file named `filename` | # - first time, it checks for a file named `filename` | ||||
# - second time, it checks for a file named `filename`_visit1 | # - second time, it checks for a file named `filename`_visit1 | ||||
# etc... | # etc... | ||||
requests_mock_datadir_visits = requests_mock_datadir_factory( | requests_mock_datadir_visits = requests_mock_datadir_factory( | ||||
has_multi_visit=True) | has_multi_visit=True) | ||||
@pytest.yield_fixture | |||||
def flask_app_client(app): | |||||
with app.test_client() as client: | |||||
yield client | |||||
# stolen from pytest-flask, required to have url_for() working within tests | |||||
# using flask_app_client fixture. | |||||
@pytest.fixture(autouse=True) | |||||
def _push_request_context(request): | |||||
"""During tests execution request context has been pushed, e.g. `url_for`, | |||||
`session`, etc. can be used in tests as is:: | |||||
def test_app(app, client): | |||||
assert client.get(url_for('myview')).status_code == 200 | |||||
""" | |||||
if 'app' not in request.fixturenames: | |||||
return | |||||
app = request.getfixturevalue('app') | |||||
ctx = app.test_request_context() | |||||
ctx.push() | |||||
def teardown(): | |||||
ctx.pop() | |||||
request.addfinalizer(teardown) |