用于测试Plone插件的pytest插件
项目描述
pytest-plone
pytest-plone 是一个 pytest 插件,提供固定和辅助工具来测试 Plone 插件。
此软件包基于 zope.pytestlayer 构建。
理由
尽管Plone和Zope的代码库使用unittest
进行测试,但随着时间的推移,pytest
已成为Python测试中最受欢迎的选择。
pytest
比unittest
更灵活、更易于使用,并且拥有丰富的插件生态系统,您可以使用这些插件来扩展其功能。
警告
此软件包被视为不稳定,并且在实际发布之前,固定装置可能会发生变化
用法
在您的顶级conftest.py
中导入您的测试层,并导入fixtures_factory
-- 它将接受一个包含测试层和用于生成所需pytest固定装置的前缀的元组迭代器。
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"),
)
)
)
在上述代码中,以下pytest固定装置将可供您的测试使用
固定装置 | 范围 |
---|---|
functional_session | 会话 |
functional_class | 类 |
functional | 函数 |
integration_session | 会话 |
integration_class | 类 |
integration | 函数 |
固定装置
generate_mo
描述 | 设置环境变量以强制Zope编译翻译文件 |
所需固定装置 | |
范围 | 会话 |
向您的conftest.py
添加一个新的固定装置,以强制对所有测试调用generate_mo
。
@pytest.fixture(scope="session", autouse=True)
def session_initialization(generate_mo):
"""Fixture used to force translation files to be compiled."""
yield
app
描述 | Zope根 |
所需固定装置 | integration |
范围 | 函数 |
def test_app(app):
"""Test portal title."""
assert app.getPhysicalPath() == ("", )
门户
描述 | 门户对象 |
所需固定装置 | integration |
范围 | 函数 |
def test_portal_title(portal):
"""Test portal title."""
assert portal.title == "Plone Site"
http_request
描述 | HTTP请求 |
所需固定装置 | integration |
范围 | 函数 |
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
描述 | 安装器浏览器视图。用于安装/卸载/检查附加组件。 |
所需固定装置 | integration |
范围 | 函数 |
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
描述 | 可用浏览器层的列表。用于测试是否已注册特定的浏览器层。 |
所需固定装置 | integration |
范围 | 函数 |
def test_browserlayer(browser_layers):
"""Test that IMyProductLayer is registered."""
from myproduct.interfaces import IMyProductLayer
assert IMyProductLayer in browser_layers
controlpanel_actions
描述 | 控制面板操作id的列表。用于测试是否已安装特定的控制面板。 |
所需固定装置 | integration |
范围 | 函数 |
def test_configlet_install(controlpanel_actions):
"""Test if control panel is installed."""
assert "myproductcontrolpanel" in controlpanel_actions
get_fti
描述 | 获取内容类型的Factory Type Info (FTI)的函数。 |
所需固定装置 | integration |
范围 | 函数 |
def test_get_fti(get_fti):
"""Test if Document fti is installed."""
assert get_fti("Document") is not None
get_behaviors
描述 | 列出内容类型的行为的函数。 |
所需固定装置 | integration |
范围 | 函数 |
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
描述 | 获取命名词汇表的函数。 |
所需固定装置 | integration |
范围 | 函数 |
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
描述 | 门户设置工具。 |
所需固定装置 | integration |
范围 | 函数 |
def test_setup_tool(setup_tool):
"""Test setup_tool."""
assert setup_tool is not None
profile_last_version
描述 | 获取配置文件的最新版本的函数。 |
所需固定装置 | integration |
范围 | 函数 |
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"
插件开发
您需要一个工作环境(系统、virtualenv、pyenv等),版本为3.8或更高。
然后使用以下命令安装依赖项和开发实例
make install
为此软件包运行测试
make test
默认情况下,我们使用6.x系列中的最新Plone版本。
许可证
该项目采用GPLv2许可。
变更日志
0.5.0 (2024-05-15)
新功能
- 添加固定装置
generate_mo
,在测试期间编译翻译文件 [@ericof] #5 - 从
gocept.pytestlayer
迁移到zope.pytestlayer
[@ericof] #11
内部
- 实现plone/meta [@ericof] #6
- 清理pytest-plone的依赖项 [@thet], [@gforcada], [@ericof] #9
- 将pytest版本锁定在8.0以下 [@ericof] #12
- 更新plone/meta [@ericof] #13
0.2.0 (2023-01-05)
-
添加
app
固定装置。 [ericof] -
添加
setup_tool
和profile_last_version
固定装置。 [ericof] -
添加
get_fti
和get_behaviors
固定装置。 [ericof] -
添加
get_vocabulary
固定装置。 [ericof]
0.1.0 (2023-01-04)
-
固定装置
portal
、http_request
、installer
、browser_layers
、controlpanel_actions
[ericof] -
首次发布 [ericof]
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。
源分发
构建分发
pytest_plone-0.5.0.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8a163f9b68015baf38c32802e83101e3fa9292a75705533fc41f1b2a434a5348 |
|
MD5 | 8667fa40901bcd14aa7cf306f9d02d04 |
|
BLAKE2b-256 | 8c0aee55e549aaeb706db0a02b4de6aaca59a51897b8465f95257ba0a25c82c4 |
pytest_plone-0.5.0-py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f9744bb0a6b60f39ba53da3921768cb4987794c7312b75eb690f6c165ffdebda |
|
MD5 | cb8c893be9cd2fe79d28d2df3a94e508 |
|
BLAKE2b-256 | 84c953523a4aa36f5804a1ca6d9ba558a46a00d79735dec9036af0474728f888 |