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 authored and zorun committed Apr 16, 2024
1 parent a5f83de commit 3ac1bb8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ihatemoney/tests/conftest.py
@@ -1,6 +1,7 @@
from unittest.mock import MagicMock

from flask import Flask
from jinja2 import FileSystemBytecodeCache
import pytest

from ihatemoney.babel_utils import compile_catalogs
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 3ac1bb8

Please sign in to comment.