| 1 |
import os |
| 2 |
from pecan import load_app |
| 3 |
|
| 4 |
|
| 5 |
def load_test_app(config=None, **kwargs): |
| 6 |
""" |
| 7 |
Used for functional tests where you need to test your |
| 8 |
literal application and its integration with the framework. |
| 9 |
|
| 10 |
:param config: Can be a dictionary containing configuration, a string which |
| 11 |
represents a (relative) configuration filename or ``None`` |
| 12 |
which will fallback to get the ``PECAN_CONFIG`` env |
| 13 |
variable. |
| 14 |
|
| 15 |
returns a pecan.Pecan WSGI application wrapped in a webtest.TestApp |
| 16 |
instance. |
| 17 |
|
| 18 |
:: |
| 19 |
app = load_test_app('path/to/some/config.py') |
| 20 |
|
| 21 |
resp = app.get('/path/to/some/resource').status_int |
| 22 |
assert resp.status_int == 200 |
| 23 |
|
| 24 |
resp = app.post('/path/to/some/resource', params={'param': 'value'}) |
| 25 |
assert resp.status_int == 302 |
| 26 |
|
| 27 |
Alternatively you could call ``load_test_app`` with no parameters if the |
| 28 |
environment variable is set :: |
| 29 |
|
| 30 |
app = load_test_app() |
| 31 |
|
| 32 |
resp = app.get('/path/to/some/resource').status_int |
| 33 |
assert resp.status_int == 200 |
| 34 |
""" |
| 35 |
# Only import at runtime, since this is a relatively heavy-weight |
| 36 |
# dependency: |
| 37 |
from webtest import TestApp |
| 38 |
return TestApp(load_app(config, **kwargs)) |
| 39 |
|
| 40 |
|
| 41 |
def reset_global_config(): |
| 42 |
""" |
| 43 |
When tests alter application configurations they can get sticky and pollute |
| 44 |
other tests that might rely on a pristine configuration. This helper will |
| 45 |
reset the config by overwriting it with ``pecan.configuration.DEFAULT``. |
| 46 |
""" |
| 47 |
from pecan import configuration |
| 48 |
configuration.set_config( |
| 49 |
dict(configuration.initconf()), |
| 50 |
overwrite=True |
| 51 |
) |
| 52 |
os.environ.pop('PECAN_CONFIG', None) |