Skip to content

Commit 3e98890

Browse files
committed
Add @pytest.mark.portal marker and supporting fixtures (#37)
Introduce a portal marker that lets tests declare GenericSetup profiles, content, and roles inline — without overriding the portal fixture. New session-scoped fixtures: apply_profiles, create_content, grant_roles. Also: bump Plone to 6.1.4, add plone-stubs to test deps, update mx.ini.
1 parent 988d255 commit 3e98890

19 files changed

Lines changed: 443 additions & 9 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ var/
4343
constraints*.txt
4444
requirements*.txt
4545
.coverage*
46+
.*_cache/
47+
48+
# Local Claude context (not for sharing)
49+
.claude/*.local.*

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ BACKEND_FOLDER=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
2828
ifdef PLONE_VERSION
2929
PLONE_VERSION := $(PLONE_VERSION)
3030
else
31-
PLONE_VERSION := 6.1.1
31+
PLONE_VERSION := 6.1.4
32+
endif
33+
34+
ifdef CI
35+
UV_VENV_ARGS :=
36+
else
37+
UV_VENV_ARGS := --python={{ cookiecutter.__supported_versions_python[0] }}
3238
endif
3339

3440
VENV_FOLDER=$(BACKEND_FOLDER)/.venv
41+
export VIRTUAL_ENV=$(VENV_FOLDER)
3542
BIN_FOLDER=$(VENV_FOLDER)/bin
3643

3744
# Environment variables to be exported

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,83 @@ def test_last_version(profile_last_version):
316316
assert version == "1000"
317317

318318
```
319+
320+
### apply_profiles
321+
322+
| | |
323+
| --- | --- |
324+
| Description | Function to apply GenericSetup profiles to a Plone site. |
325+
| Required Fixture | **integration** |
326+
| Scope | **Session** |
327+
328+
```python
329+
def test_with_profile(portal, apply_profiles):
330+
"""Test that a profile can be applied."""
331+
apply_profiles(portal, ["my.addon:testing"])
332+
```
333+
334+
### create_content
335+
336+
| | |
337+
| --- | --- |
338+
| Description | Function to create content items in a Plone site as the site owner. |
339+
| Required Fixture | **integration** |
340+
| Scope | **Session** |
341+
342+
```python
343+
def test_with_content(portal, create_content):
344+
"""Test that content is created."""
345+
create_content(portal, [
346+
{"type": "Document", "id": "doc1", "title": "A Document"},
347+
])
348+
assert "doc1" in portal
349+
```
350+
351+
### grant_roles
352+
353+
| | |
354+
| --- | --- |
355+
| Description | Function to grant local roles to the test user on a given context. |
356+
| Required Fixture | **integration** |
357+
| Scope | **Session** |
358+
359+
```python
360+
def test_manager_action(portal, grant_roles):
361+
"""Test an action that requires Manager role."""
362+
grant_roles(portal, ["Manager"])
363+
# test user now has Manager role on portal
364+
```
365+
366+
## Markers
367+
368+
### @pytest.mark.portal
369+
370+
Configure the `portal` fixture with GenericSetup profiles, pre-created content, and/or user roles — without overriding the fixture.
371+
372+
| Parameter | Type | Description |
373+
| --- | --- | --- |
374+
| `profiles` | `list[str]` | GenericSetup profile IDs to apply (e.g. `["my.addon:testing"]`) |
375+
| `content` | `list[dict]` | Dicts passed as keyword arguments to `plone.api.content.create` |
376+
| `roles` | `list[str]` | Roles granted to the test user via `plone.api.user.grant_roles` |
377+
378+
Setup is applied in order: **profiles → content → roles**.
379+
380+
```python
381+
import pytest
382+
383+
384+
@pytest.mark.portal(
385+
profiles=["my.addon:testing"],
386+
content=[{"type": "Document", "id": "doc1", "title": "Doc 1"}],
387+
roles=["Manager"],
388+
)
389+
def test_something(portal):
390+
"""Test with custom portal setup."""
391+
assert "doc1" in portal
392+
```
393+
394+
Tests without the marker see no behavior change — fully backwards-compatible.
395+
319396
## Plugin Development
320397

321398
You need a working `python` environment (system, virtualenv, pyenv, etc) version 3.8 or superior.

mx.ini

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ version-overrides =
1212
zope.pytestlayer>=8.3
1313
pytest>=8.4.0
1414

15-
; example section to use packages from git
16-
; [example.contenttype]
17-
; url = https://github.com/collective/example.contenttype.git
18-
; pushurl = git@github.com:collective/example.contenttype.git
19-
; extras = test
20-
; branch = feature-7
15+
[plone-stubs]
16+
url = https://github.com/plone/plone-stubs.git
17+
pushurl = git@github.com:plone/plone-stubs.git

news/+apply_profiles.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `apply_profiles` session-scoped fixture to apply GenericSetup profiles to a Plone site. @ericof

news/+create_content.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `create_content` session-scoped fixture to create content items in a Plone site as the site owner. @ericof

news/+grant_roles.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `grant_roles` session-scoped fixture to grant local roles to the test user on a given context. @ericof

news/+housekeeping.internal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated development tooling: bumped Plone version to 6.1.4, added plone-stubs to test dependencies, updated mx.ini and .gitignore. @ericof

news/37.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `@pytest.mark.portal` marker support for configuring the `portal` fixture with GenericSetup profiles, pre-created content, and user roles. @ericof

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dependencies = [
4646

4747
[project.optional-dependencies]
4848
test = [
49+
"plone-stubs",
4950
"pytest-cov",
5051
"pytest-xdist",
5152
"mypy>=1.15.0",

0 commit comments

Comments
 (0)