Skip to main content

Pytest plugin to test Plone addons

Project description

pytest-plone

PyPI PyPI - Python Version PyPI - Wheel PyPI - License PyPI - Status

PyPI - Plone Versions

Tests Code Style

GitHub contributors GitHub Repo stars

pytest-plone is a pytest plugin providing fixtures and helpers to test Plone add-ons.

This package is built on top of zope.pytestlayer.

Reasoning

Despite the fact Plone, and Zope, have their codebases tested with unittest, over the years pytest became the most popular choice for testing in Python.

pytest is more flexible and easier to use than unittest and has a rich ecosystem of plugins that you can use to extend its functionality.

Warning

THIS PACKAGE IS NOT CONSIDERED TO BE STABLE AND FIXTURES COULD CHANGE BEFORE A FINAL RELEASE

Usage

In your top-level conftest.py import your testing layers, and also import fixtures_factory -- which will accept a iterator of tuples containing the testing layer and a prefix to be used to generate the needed pytest fixtures.

from Products.CMFPlone.testing import PRODUCTS_CMFPLONE_FUNCTIONAL_TESTING
from Products.CMFPlone.testing import PRODUCTS_CMFPLONE_INTEGRATION_TESTING
from pytest_plone import fixtures_factory


pytest_plugins = ["pytest_plone"]


globals().update(
    fixtures_factory(
        (
            (PRODUCTS_CMFPLONE_FUNCTIONAL_TESTING, "functional"),
            (PRODUCTS_CMFPLONE_INTEGRATION_TESTING, "integration"),
        )
    )
)

In the code above, the following pytest fixtures will be available to your tests:

Fixture Scope
functional_session Session
functional_class Class
functional Function
integration_session Session
integration_class Class
integration Function

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.

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

app

Description Zope root
Required Fixture integration
Scope Function
def test_app(app):
    """Test portal title."""
    assert app.getPhysicalPath() == ("", )

portal

Description Portal object
Required Fixture integration
Scope Function
def test_portal_title(portal):
    """Test portal title."""
    assert portal.title == "Plone Site"

http_request

Description HTTP Request
Required Fixture integration
Scope Function
from plone import api


def test_myproduct_controlpanel_view(portal, http_request):
    """Test myproduct_controlpanel browser view is available."""
    view = api.content.get_view(
        "myproduct-controlpanel", portal, http_request
    )
    assert view is not None

installer

Description Installer browser view. Used to install/uninstall/check for an add-on.
Required Fixture integration
Scope Function
import pytest


PACKAGE_NAME = "myproduct"


@pytest.fixture
def uninstall(installer):
    """Fixture to uninstall a package."""
    installer.uninstall_product(PACKAGE_NAME)


def test_product_installed(installer):
    """Test if myproduct is installed."""
    assert installer.is_product_installed(PACKAGE_NAME) is True

@pytest.mark.parametrize(
    "package",
    [
        "collective.casestudy",
        "pytest-plone",
    ]
)
def test_dependency_installed(installer, package):
    """Test if dependency is installed."""
    assert installer.is_product_installed(package) is True

browser_layers

Description List of available browser layers. Used to test if a specific browser layer is registered.
Required Fixture integration
Scope Function
def test_browserlayer(browser_layers):
    """Test that IMyProductLayer is registered."""
    from myproduct.interfaces import IMyProductLayer


    assert IMyProductLayer in browser_layers

controlpanel_actions

Description List of control panel actions ids. Used to test if a specific control panel is installed or not.
Required Fixture integration
Scope Function
def test_configlet_install(controlpanel_actions):
    """Test if control panel is installed."""
    assert "myproductcontrolpanel" in controlpanel_actions

get_fti

Description Function to get the Factory Type Info (FTI) for a content type.
Required Fixture integration
Scope Function
def test_get_fti(get_fti):
    """Test if Document fti is installed."""
    assert get_fti("Document") is not None

get_behaviors

Description Function to list behaviors for a content type.
Required Fixture integration
Scope Function
import pytest


def test_block_in_document(get_behaviors):
    """Test if blocks behavior is installed for Document."""
    assert "volto.blocks" in get_behaviors("Document")


@pytest.mark.parametrize(
    "behavior",
    [
        "plone.dublincore",
        "plone.namefromtitle",
        "plone.shortname",
        "plone.excludefromnavigation",
        "plone.relateditems",
        "plone.versioning",
        "volto.blocks",
        "volto.navtitle",
        "volto.preview_image",
        "volto.head_title",
    ],
)
def test_has_behavior(self, get_behaviors, behavior):
    assert behavior in get_behaviors("Document")

get_vocabulary

Description Function to get a named vocabulary.
Required Fixture integration
Scope Function
from zope.schema.vocabulary import SimpleVocabulary

VOCAB = "plone.app.vocabularies.AvailableContentLanguages"

def test_get_vocabulary(get_vocabulary):
    """Test plone.app.vocabularies.AvailableContentLanguages."""
    vocab = get_vocabulary(VOCAB)
    assert vocab is not None
    assert isinstance(vocab, SimpleVocabulary)

setup_tool

Description Portal Setup tool.
Required Fixture integration
Scope Function
def test_setup_tool(setup_tool):
    """Test setup_tool."""
    assert setup_tool is not None

profile_last_version

Description Function to get the last version of a profile.
Required Fixture integration
Scope Function
PACKAGE_NAME = "collective.case_study"

def test_last_version(profile_last_version):
    """Test setup_tool."""
    profile = f"{PACKAGE_NAME}:default"
    version = profile_last_version(profile)
    assert version == "1000"

Plugin Development

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

Then install the dependencies and a development instance using:

make install

To run tests for this package:

make test

By default we use the latest Plone version in the 6.x series.

License

The project is licensed under the GPLv2.

Changelog

0.5.0 (2024-05-15)

New features:

  • Add fixture generate_mo to compile translation files during tests [@ericof] #5
  • Move from gocept.pytestlayer to zope.pytestlayer [@ericof] #11

Internal:

  • Implement plone/meta [@ericof] #6
  • Clean up dependencies for pytest-plone [@thet], [@gforcada], [@ericof] #9
  • Pin pytest version to be lower than 8.0 [@ericof] #12
  • Update plone/meta [@ericof] #13

0.2.0 (2023-01-05)

  • Add app fixture. [ericof]

  • Add setup_tool and profile_last_version fixtures. [ericof]

  • Add get_fti and get_behaviors fixtures. [ericof]

  • Add get_vocabulary fixture. [ericof]

0.1.0 (2023-01-04)

  • Fixtures portal, http_request, installer, browser_layers, controlpanel_actions [ericof]

  • Initial release [ericof]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_plone-0.5.0.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

pytest_plone-0.5.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_plone-0.5.0.tar.gz.

File metadata

  • Download URL: pytest_plone-0.5.0.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for pytest_plone-0.5.0.tar.gz
Algorithm Hash digest
SHA256 8a163f9b68015baf38c32802e83101e3fa9292a75705533fc41f1b2a434a5348
MD5 8667fa40901bcd14aa7cf306f9d02d04
BLAKE2b-256 8c0aee55e549aaeb706db0a02b4de6aaca59a51897b8465f95257ba0a25c82c4

See more details on using hashes here.

File details

Details for the file pytest_plone-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_plone-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for pytest_plone-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9744bb0a6b60f39ba53da3921768cb4987794c7312b75eb690f6c165ffdebda
MD5 cb8c893be9cd2fe79d28d2df3a94e508
BLAKE2b-256 84c953523a4aa36f5804a1ca6d9ba558a46a00d79735dec9036af0474728f888

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page