websauna.tests.webserver module

py.test fixtures for spinning up a WSGI server for functional test run.

websauna.tests.webserver.customized_web_server(request, app, customized_port=None)[source]

py.test fixture to create a WSGI web server for functional tests with custom INI options set.

This is similar to web_server, but instead directly spawning a server, it returns a factory method which you can use to launch the web server which custom parameters besides those given in test.ini.

Example:

def test_newsletter_splash(dbsession, customized_web_server, browser):
    """All visitors get a newsletter subscription dialog when arriving to the landing page.

    :param dbsession: py.test fixture for
    :param ini_settings: py.test fixture for loading INI settings from command line
    """

    # Create a WSGI server where newsletter splash dialog is explicitly enabled (disabled by default for testing)
    web_server = customized_web_server({"trees.newsletter_splash": True})

    b = browser
    b.visit(web_server)

    # Scroll down a bit to trigger the dialog
    browser.driver.execute_script("window.scrollTo(0, 10)")

    # Wait the dialog to come up
    time.sleep(2)

Another example where we create one web server with INI overrides through the one test module. The benefit of this approach is that if you need to share the settings overrides across the tests, only one web server gets created instead of a new server for each test:

import pytest

from decimal import Decimal
from _pytest.python import FixtureRequest
from pyramid.router import Router

import transaction

from websauna.system.model import now
from websauna.tests.fixtures import customized_web_server


@pytest.fixture(scope='module')
def choose_box_web_server(request:FixtureRequest, app:Router) -> str:
    """A py.test fixture to give you a functional web server with specific INI settings overridden.

    In this case, we enable box selection screen on checkout process to test it out.
    """
    web_server_factory = customized_web_server(request, app)

    # Flip trees.choose_box settings from default test.ini value
    return web_server_factory({"trees.choose_box": True})


def test_order_free_foxyboxy(choose_box_web_server, browser, dbsession, init):
    """Fresh user places an order and gets foxyboxy for free.."""

    web_server = choose_box_web_server

    purge_uploads(init)
    purge_redis(init)

    with transaction.manager:
        user = create_user(dbsession)
        spoof_license(init, user)
        user.dispensary_membership_confirmed_at = now()

    b = browser

    b.visit("{}/cannabis-bud-box".format(web_server))

    b.find_by_css(".btn-buy-now").click()

    b.fill("email", EMAIL)
    b.find_by_name("login_email").click()
    verify_email_login(web_server, browser, init, EMAIL)

    b.find_by_css("input[value='box_foxyboxy']").click()
    b.find_by_name("next").click()

    fill_in_delivery_details(b, phone_number="+358407439707")
    confirm_delivery(b)

    assert b.is_element_present_by_css("#thank-you")


def test_order_non_free_foxyboxy(choose_box_web_server, browser, dbsession, init):
    """Fresh user places the second order and gets foxyboxy for free.."""
    web_server = choose_box_web_server
    ...

Inspiration: http://stackoverflow.com/a/28570677/315168

Parameters:
  • app (Router) – py.test fixture for constructing a WSGI application
  • port – Force a certain port.
Return type:

Callable

Returns:

A factory callable you can use to spawn a web server. Pass test.ini overrides as dict to this function.

websauna.tests.webserver.web_server(request, app)[source]

py.test fixture to create a WSGI web server for functional tests.

The default web server address is localhost:8521. The port can be changed with websauna.test_web_server_port.

Parameters:app (Router) – py.test fixture for constructing a WSGI application
Return type:str
Returns:localhost URL where the web server is running.