Skip to content

Commit

Permalink
Merge pull request #8 from collective/issue-5-fixture-generate-mo
Browse files Browse the repository at this point in the history
Add fixture  to compile translation files during tests
  • Loading branch information
ericof authored Dec 5, 2023
2 parents a362d9e + d6de0ce commit f2bbb6c
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 5 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

[![PyPI - Plone Versions](https://img.shields.io/pypi/frameworkversions/plone/pytest-plone)](https://pypi.org/project/pytest-plone/)

[![Code analysis checks](https://github.com/collective/pytest-plone/actions/workflows/code-analysis.yml/badge.svg)](https://github.com/collective/pytest-plone/actions/workflows/code-analysis.yml)
[![Tests](https://github.com/collective/pytest-plone/actions/workflows/tests.yml/badge.svg)](https://github.com/collective/pytest-plone/actions/workflows/tests.yml)
[![Tests](https://github.com/collective/pytest-plone/actions/workflows/meta.yml/badge.svg)](https://github.com/collective/pytest-plone/actions/workflows/meta.yml)
![Code Style](https://img.shields.io/badge/Code%20Style-Black-000000)

[![GitHub contributors](https://img.shields.io/github/contributors/collective/pytest-plone)](https://github.com/collective/pytest-plone)
Expand Down Expand Up @@ -72,6 +71,25 @@ In the code above, the following pytest fixtures will be available to your tests

## Fixtures

### generate_mo

| | |
| --- | --- |
| Description | Set environment variable to force Zope to compile translation files |
| Required Fixture | |
| Scope | **Session** |

Add a new fixture to your `conftest.py` to force `generate_mo` to be called for all tests.

```python

@pytest.fixture(scope="session", autouse=True)
def session_initialization(generate_mo):
"""Fixture used to force translation files to be compiled."""
yield

```

### app

| | |
Expand Down
1 change: 1 addition & 0 deletions news/5.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add fixture `generate_mo` to compile translation files during tests [@ericof]
2 changes: 2 additions & 0 deletions src/pytest_plone/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .base import portal
from .content import get_behaviors
from .content import get_fti
from .env import generate_mo
from .vocabularies import get_vocabulary


Expand All @@ -17,6 +18,7 @@
browser_layers,
controlpanel_actions,
get_behaviors,
generate_mo,
get_fti,
get_vocabulary,
http_request,
Expand Down
23 changes: 23 additions & 0 deletions src/pytest_plone/fixtures/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Environment fixtures."""
import os
import pytest


@pytest.fixture(scope="session")
def generate_mo():
"""Compile translation files and generate .mo files.
This is a session fixture, as it needs to compile files just once.
"""
# Set environment variable
key = "zope_i18n_compile_mo_files"
current_value = os.getenv(key, None)
os.environ[key] = "1"
try:
yield
finally:
# Revert to previous state
if current_value is None:
os.environ.pop(key)
else:
os.environ[key] = current_value
30 changes: 30 additions & 0 deletions tests/env/test_env_generate_mo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
TEST_FILES = {
"test_generate_mo_result_no_env.py": """
import os
def test_generate_mo_result(generate_mo):
assert os.getenv("zope_i18n_compile_mo_files") == "1"
""",
"test_generate_mo_result_with_env.py": """
import pytest
import os
@pytest.fixture(scope="session", autouse=True)
def set_i18n_var():
os.environ["zope_i18n_compile_mo_files"] = "2"
def test_generate_mo_result():
assert os.getenv("zope_i18n_compile_mo_files") == "2"
""",
}


def test_generate_mo(testdir):
"""Test generate_mo adds environment variable."""
testdir.makepyfile(**TEST_FILES)
# run all tests with pytest
result = testdir.runpytest()
# check that all tests passed
result.assert_outcomes(passed=len(TEST_FILES))
9 changes: 6 additions & 3 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ def test_all_fixtures(testdir):
"app",
"browser_layers",
"controlpanel_actions",
"generate_mo",
"get_behaviors",
"get_fti",
"get_vocabulary",
"http_request",
"installer",
"profile_last_version",
"portal",
"profile_last_version",
"setup_tool",
]
pyfile = ""
for fixture in fixtures:
pyfile = f"""
{pyfile}
def test_{fixture}({fixture}):
assert {fixture} is not None
def test_{fixture}_exists({fixture}):
# Just pass the test, because if a fixture is not available,
# it will raise an error
assert True
"""

Expand Down

0 comments on commit f2bbb6c

Please sign in to comment.