Skip to content

Instantly share code, notes, and snippets.

@MatthewWilkes
Created May 8, 2019 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MatthewWilkes/352bfdcf222e23d8f19943565b585a1e to your computer and use it in GitHub Desktop.
Save MatthewWilkes/352bfdcf222e23d8f19943565b585a1e to your computer and use it in GitHub Desktop.
Pyramid functional testing example
from contextlib import contextmanager
import tempfile
from unittest import mock
import factory
import pytest
from betterevidence import models
@pytest.fixture(scope="session")
def engine(app_settings):
from betterevidence.models.meta import Base
from betterevidence.models import get_engine
engine = get_engine(app_settings)
Base.metadata.create_all(engine)
return engine
@pytest.fixture
def connection(engine):
connection = engine.connect()
yield connection
connection.close()
@pytest.fixture
def transaction(connection):
transaction = connection.begin()
yield transaction
transaction.rollback()
@pytest.fixture()
def session(app_settings, connection, transaction):
from sqlalchemy.orm import sessionmaker
Session = sessionmaker()
session = Session(bind=connection)
return session
@pytest.fixture(scope="session")
def app_settings():
yield {"secret": "secret", "sqlalchemy.url": "sqlite://"}
@pytest.fixture(scope="session")
def root_app_inner(app_settings):
from betterevidence import main
app = main({}, **app_settings)
app.registry["dbsession_factory"].kw["bind"] = connection
yield app
@pytest.fixture
def root_app(root_app_inner, connection):
root_app_inner.registry["dbsession_factory"].kw["bind"] = connection
yield root_app_inner
@pytest.fixture
def webtest(root_app, session):
from webtest import TestApp
return TestApp(root_app)
def test_sites_json_returns_empty_list_if_no_sites(self, webtest):
result = webtest.get("/sites.json")
assert result.json == []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment