Testing the Framework
Table of contents
- Introduction
- Running the framework’s own tests
- The fixtures features use
- How the fixtures find the product
- Running a feature’s tests
- See also
Introduction
splent_framework has its own unit suite under tests/, independent of any product. It also ships the pytest fixtures and helpers every feature’s tests use. This page covers both, for contributors to the framework.
cd /workspace/splent_framework
pip install -e ".[dev]"
pytest
Running the framework’s own tests
The dev extra brings pytest and pytest-cov, and pyproject.toml sets testpaths = ["tests"] and pythonpath = ["src"].
pytest --cov=splent_framework --cov-report=term-missing
The tests of the testing helpers stub locust, selenium, webdriver_manager and faker through sys.modules, so the suite runs without those packages. The product’s development image installs the framework the same way, pip install -e "./splent_framework[dev]", which is where feature:test gets pytest.
The fixtures features use
# tests/conftest.py of a feature
from splent_framework.fixtures.fixtures import * # noqa: F401,F403
| Fixture | Scope | Gives |
|---|---|---|
test_app |
Session | The product’s app in testing mode, built once |
test_client |
Function | A test client with a clean database for every test |
test_client_module |
Module | A test client whose database resets once per file |
clean_database |
Function | A reset inside a test, without a client |
no_access_policies |
Function | A test with no access policies registered |
The first reset in each process turns foreign key checks off, drops every table of the test database (not only the app’s, since other apps share it) except splent_migrations, and creates the app’s tables again. It then records a digest of the schema from information_schema. Later resets compare the schema with that digest and, if it matches, only delete the rows of the app’s tables and restart the id counters that moved. A different schema gets the full reset. Databases other than MariaDB and MySQL always get the full reset. Fixtures shows how a feature uses them.
The auth helpers sign a test client in and out.
from splent_framework.helpers.test_helpers_auth import login, logout
def test_protected_route(test_client):
login(test_client, "user@example.com", "secret")
assert test_client.get("/notes/create").status_code == 200
logout(test_client)
How the fixtures find the product
get_create_app_in_testing_mode(), in splent_framework.utils.app_loader, imports the module SPLENT_APP names and calls its create_app("testing"). It loads the product’s .env first and puts its src/ on sys.path.
Precedence is shell environment, then the product’s .env, then the workspace .env, as in the CLI. A variable you export, such as MARIADB_TEST_DATABASE in CI, is kept. A missing module, or one without create_app(), raises a RuntimeError that names it. The loader never imports the CLI, so the fixtures work in any environment with the product, its features and a test database.
Running a feature’s tests
splent feature:test notes # one feature, default levels
splent feature:test notes --integration -v # one level, verbose
feature:test runs the suite inside the selected product’s web container, so start it first with splent product:up --dev. A full feature’s scaffold has every test level. A light one has unit/ and functional/, and config and service features have unit/. Add the other folders when you need them. See Feature testing.
See also
feature:test, the command reference- Application initialization,
TestingConfig