websauna.tests.fixtures module¶
Various Websauna specific py.test fixtures.
Reading test.ini settings
Setting up and tearing down database
Creating a WSGI application to test
-
websauna.tests.fixtures.
app
(request, ini_settings, **settings_overrides)[source]¶ Initialize WSGI application from INI file given on the command line.
- Parameters
settings_overrides – Override specific settings for the test case.
- Return type
Router
- Returns
WSGI application instance as created by
Initializer.make_wsgi_app()
. You can access the Initializer instance itself asapp.initializer
.
-
websauna.tests.fixtures.
create_test_dbsession
(request, registry, transaction_manager=<transaction._manager.ThreadTransactionManager object>)[source]¶ Create a test database session and setup database.
Create and drop all tables when called. Add teardown function py.test to drop all tables during teardown. Also add implicit UUID extension on the database, so we don’t need to add by hand every time.
- Parameters
request – py.test test request
settings – test.ini app settings
transaction_manager –
- Return type
- Returns
New database session
-
websauna.tests.fixtures.
dbsession
(request, app)[source]¶ Create a test database and database session.
Connect to the test database specified in test.ini. Create and destroy all tables by your Initializer configuration.
Performs SQLAlchemy table initialization for all models connected to
websauna.system.model.meta.Base
. You must do manual opening and closing transaction inside the test, preferably usingtransaction.manager
context manager. If transaction is left open, subsequent tests may fail.Example:
import transaction from trees.models import NewsletterSubscriber def test_subscribe_newsletter(dbsession): '''Visitor can subscribe to a newsletter.''' # ... test code goes here ... # Check we get an entry with transaction.manager: assert dbsession.query(NewsletterSubscriber).count() == 1 subscription = dbsession.query(NewsletterSubscriber).first() assert subscription.email == "[email protected]" assert subscription.ip == "127.0.0.1"
- Return type
- Returns
A SQLAlchemy session instance you can use to query database.
-
websauna.tests.fixtures.
get_app
(ini_settings, extra_init=None)[source]¶ Construct a WSGI application from INI settings.
You can pass extra callable which is called back when Initializer is about to finish. This allows you to poke app configuration easily.
- Return type
Router
-
websauna.tests.fixtures.
http_request
(request)[source]¶ Dummy HTTP request for testing.
For spoofing link generation, routing, etc.
-
websauna.tests.fixtures.
ini_settings
(request, test_config_path)[source]¶ Load INI settings for test run from py.test command line.
Example:
py.test yourpackage -s –ini=test.ini
- Return type
- Returns
A dictionary representing the key/value pairs in an
app
section within the file represented byconfig_uri
-
websauna.tests.fixtures.
init
(request, app)[source]¶ Access to the default
websauna.system.Initializer
instance created fromtest.ini
.
-
websauna.tests.fixtures.
paster_config
(request, test_config_path, ini_settings)[source]¶ A fixture to get global config and app settings for Paster-like passing.
This is mostly useful cases where your test needs to ramp up its own
websauna.system.Initializer
.Example:
def test_customize_login(paster_config): '''Customizing login form works.''' class CustomLoginForm(DefaultLoginForm): def __init__(self, *args, **kwargs): login_button = Button(name="login_email", title="Login by fingerprint", css_class="btn-lg btn-block") kwargs['buttons'] = (login_button,) super().__init__(*args, **kwargs) class Initializer(websauna.system.Initializer): def configure_user_forms(self): from websauna.system.user import interfaces # This will set up all default forms as shown in websauna.system.Initializer.configure_user_forms super(Initializer, self).configure_user_forms() # Override the default login form with custom one unregister_success= self.config.registry.unregisterUtility(provided=interfaces.ILoginForm) assert unregister_success, "No default form register" self.config.registry.registerUtility(CustomLoginForm, interfaces.ILoginForm) global_config, app_settings = paster_config init = Initializer(global_config, app_settings) init.run() app = TestApp(init.make_wsgi_app()) resp = app.get("/login")
- Return type
-
websauna.tests.fixtures.
registry
(request, app)[source]¶ Get access to registry.
Effectively returns
app.initializer.config.registry
. Registry is a Pyramid registry that is populated with values from test.ini.Example:
import transaction from websauna.tests.test_utils import create_user def test_some_stuff(dbsession, registry): with transaction.manager: u = create_user(registry) # Do stuff with new user
- Return type
Registry
-
websauna.tests.fixtures.
scaffold_webdriver
()[source]¶ A fixture to get Webdriver settings from external environment to be used inside scaffold tests.
TODO: This fixture does not serve purpose outside Websauna and should be moved somewhere.
-
websauna.tests.fixtures.
test_config_path
(request)[source]¶ A py.test fixture to get test INI configuration file path from py.test command line.
- Return type
- Returns
Absolute path to test.ini file
-
websauna.tests.fixtures.
test_request
(request, dbsession, registry)[source]¶ Create a dummy HTTP request object which can be used to obtain services and adapters.
This fixture gives you an instance of
pyramid.testing.DummyRequest
object which looks like a request as it would have arrived through HTTP interface. It has request-like properties, namelyregistry
dbsession
… and thus can be used to access services, utilies and such which normally would take a request as an argument.
Example:
from websauna.system.user.utils import get_login_service def test_order(dbsession, test_request): service = get_login_service(test_request)
The
request.tm
is bound to thread-localtransaction.mananger
.- Return type
InterfaceClass