Skip to content

Commit

Permalink
tests: cache the jinja bytecode between unit tests
Browse files Browse the repository at this point in the history
The jinja templates are compiled once per test session instead of once
per test, using jinja cache system and a pytest fixture.

https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.FileSystemBytecodeCache
  • Loading branch information
azmeuk committed Apr 16, 2024
1 parent a5f83de commit 5876ae5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ihatemoney/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest.mock import MagicMock
from jinja2 import FileSystemBytecodeCache

from flask import Flask
import pytest
Expand All @@ -13,11 +14,19 @@ def babel_catalogs():
compile_catalogs()


@pytest.fixture(scope="session")
def jinja_cache_directory(tmp_path_factory):
return tmp_path_factory.mktemp("cache")


@pytest.fixture
def app(request: pytest.FixtureRequest):
def app(request: pytest.FixtureRequest, jinja_cache_directory):
"""Create the Flask app with database"""
app = create_app(request.cls)

# Caches the jinja templates so they are compiled only once per test session
app.jinja_env.bytecode_cache = FileSystemBytecodeCache(jinja_cache_directory)

with app.app_context():
db.create_all()
request.cls.app = app
Expand Down

0 comments on commit 5876ae5

Please sign in to comment.