端到端测试套件
这个测试套件确保新的拉取请求不会破坏 Mautic 的任何功能,并有助于维护应用程序的整体质量。它使用 Codeception,一个流行的 PHP 测试框架,以及 Selenium 用于浏览器自动化。
设置
本指南假设您的 Mautic 项目已经安装并在 DDEV 上运行。如果不是,请按照以下步骤操作:
克隆仓库:
git clone <repository-url>
cd <repository-name>
启动 DDEV:
确保您已在系统上安装了 DDEV。使用以下命令启动 DDEV 环境:
ddev start
有关详细步骤,请参阅 Mautic 文档。
构建测试依赖项:
bin/codecept build
codeception.yml 和 tests/acceptance.suite.yml 配置文件已经就位。
配置测试环境
在验收测试中,您的测试通过 Web 服务器与应用程序交互,并使用与应用程序相同的数据库。为了避免在测试期间修改实际的应用程序数据库,您应该配置一个单独的测试数据库。这种设置确保测试环境不会影响您的生产数据,并允许进行隔离测试。
每次需要运行测试时,请确保更新 .env.local 以启用测试模式。
编辑 .env.local:
将环境设置为测试模式。
# .env.local
APP_ENV=test
APP_DEBUG=1
配置测试数据库凭据:
确保您的 .env.test.local 文件包含用于测试数据库的正确凭据。
# .env.test.local
DB_HOST=db
DB_USER=db
DB_PASSWD=db
DB_NAME=test
Mautic 使用 db 数据库进行生产,并使用 test 数据库运行测试。
验收测试结构
tests/ 目录包含所有测试,根目录下有 codeception.yml 文件。这些测试使用 WebDriver 和 Db 模块,配置信息在 acceptance.suite.yml 中指定。 Codeception 使用 W3C WebDriver 协议在真实浏览器中运行测试,Selenium 管理浏览器交互。
以下是测试目录结构的概述:
- Directory
Description
_data/Contains fixture data used in tests, including SQL dump files and sample CSV files.
_output/Contains output from tests in case of failures. This includes snapshots of the browser in JPEG format and generated HTML reports for troubleshooting.
_support/AcceptanceTester.php: contains login logic that runs before each test.Helper/: stores custom helper functions. For example,DbHelper.phpautomates the process of generating SQL dump files and populating the database. It prepares the database from scratch if no dump file exists, and exports a SQL file for future use.Page/: stores UI locators for each page. Avoid hard-coding complex CSS or XPath locators in tests; instead, use PageObject classes.Step/: contains step objects that group common functionalities for tests.
acceptance/Contains acceptance tests.
Writing and running tests
Writing tests
Writing tests in Codeception involves creating files within the tests/Acceptance directory. Each file contains a class with methods that define the test scenarios.
Create a New Test File
Use the following command to generate a new file:
bin/codecept generate:cest acceptance <TestName>
This creates a TestSuiteNameCest.php file in tests/Acceptance.
Define Test Scenarios
Open the generated file and define your test scenarios. Each method within the class represents a different scenario. Use Codeception’s built-in assertions and helper functions to verify the expected outcomes. Here’s an example:
<?php
class TestSuiteNameCest
{
public function _before(AcceptanceTester $I)
{
// Code to run before each test
}
public function _after(AcceptanceTester $I)
{
// Code to run after each test
}
// Define your test methods
public function login(AcceptanceTester $I)
{
$I->amOnPage('/s/login');
$I->fillField('#username', $name);
$I->fillField('#password', $password);
$I->click('button[type=submit]');
$I->see('Dashboard');
}
}
Utilize PageObjects and StepObjects
Organize your tests by using PageObject and StepObject classes. This keeps your tests clean and maintainable by separating locators and test steps into reusable components.
Generate a page object with:
bin/codecept generate:pageobject acceptance ExamplePage
This creates an ExamplePage.php file in /tests/Support/Page/Acceptance.
Generate step objects with:
bin/codecept generate:stepobject acceptance Example
This creates an Example.php file in /tests/Support/Step/Acceptance.
Running tests
您可以使用 Codeception 提供的 run 命令来启动测试。以下是运行测试的几种方法:
运行所有测试
bin/codecept run
运行所有验收测试
bin/codecept run acceptance
运行特定测试文件
如果需要运行特定的测试文件,例如 ContactManagementCest,请使用:
bin/codecept run acceptance ContactManagementCest
运行特定测试场景
要运行测试文件中的特定场景,您可以指定测试方法,如下所示:
bin/codecept run acceptance ContactManagementCest:createContactFromForm
查看测试结果
在运行测试后,Codeception 会在终端中返回结果。此外,任何失败都会生成快照和 HTML 报告,这些报告位于 _output 目录中,可用于调试。
其他选项
打印步骤:
要查看测试执行的逐步分解,请使用:
bin/codecept run acceptance ContactManagementCest --steps
详细输出
要获取更详细的内部调试信息,请使用:
bin/codecept run acceptance ContactManagementCest -vvv
在浏览器中查看测试
您可以通过访问以下 URL 在自动化浏览器中观看您的测试运行情况:https://mautic.ddev.site:7900/。
noVNC 访问:
密码:secret
贡献
欢迎对测试套件做出贡献。请遵循提交拉取请求的指南,具体内容请参阅 Tester Documentation。