dingo-d/wp-pest-integration-test-setup

此包已被废弃,不再维护。作者建议使用 dingo-d/wp-pest 包。

一个将添加WordPress集成测试套件(使用Pest框架)的包

2.0.0-alpha 2023-09-20 09:02 UTC

README

使用PestPHP进行WordPress集成测试

一个包,可以将WordPress集成和单元测试套件添加到使用 Pest PHP 测试框架。

为什么要这样做?

当提到自动化测试时,WordPress 并不是第一个想到的,对吧?希望这个包能帮助打破WordPress测试的偏见。

此包将使您能够使用Pest PHP框架轻松且可读的测试设置快速启动。

要求

  1. PHP > 7.4
  2. Composer

此包仅与Composer一起工作,我计划不支持其他安装方式。

设置

在您的项目中运行

composer require dingo-d/wp-pest-integration-test-setup --dev

之后,您可以运行以下命令

vendor/bin/wp-pest setup theme

这将设置 tests 文件夹,下载最新版本的 WordPress develop 仓库并将其放置在 wp 文件夹中。它还将使用您主题中可以运行的示例设置您的集成和单元测试套件。

您可以通过输入以下选项进行其他选择

vendor/bin/wp-pest setup --help
Description:
  Sets up the test suites.

Usage:
  setup [options] [--] <project-type>

Arguments:
  project-type                     Select whether you want to setup tests for theme or a plugin. Can be "theme" or "plugin"

Options:
      --wp-version[=WP-VERSION]    Pass the version of the WordPress you want to test on. [default: "latest"]
      --plugin-slug[=PLUGIN-SLUG]  If you are setting the plugin tests provide the plugin slug.
      --skip-delete                If you are running the setup tests in a CI pipeline, provide this option to skip the deletion step.
  -h, --help                       Display help for the given command. When no command is given display help for the list command
  -q, --quiet                      Do not output any message
  -V, --version                    Display this application version
      --ansi|--no-ansi             Force (or disable --no-ansi) ANSI output
  -n, --no-interaction             Do not ask any interactive question
  -v|vv|vvv, --verbose             Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  This command helps you set up WordPress integration and unit test suites.

内部原理

要深入了解这个包是如何产生的,您可以阅读 这篇文章

基本上,“内部原理”是从 wordpress-develop 仓库下载到您的项目中,添加了内存数据库(来自 aaemnnosttv/wp-sqlite-db 的 sql lite)和来自 Yoast/wp-test-utils 的基础测试类。所有这些组合允许您在WordPress中使用Pest PHP运行集成测试而无需任何额外设置。

在集成测试的同时运行单元测试

由于Pest处理 文件加载 的bug,为了成功运行单元测试,您需要将以下助手添加到您的 Pest.php 文件或 Helpers.php 文件中

function isUnitTest() {
	return !empty($GLOBALS['argv']) && $GLOBALS['argv'][1] === '--group=unit';
}

然后,在您的 集成 测试中,您需要在测试用例的 uses() 调用之前添加以下内容

<?php

use Yoast\WPTestUtils\WPIntegration\TestCase;

if (isUnitTest()) {
	return;
}

uses(TestCase::class);

// Rest of the tests.

这样,当您运行单元测试组时,集成测试文件将退出,您不会错误地使用测试类进行测试。

测试示例

命令将设置两个示例 - 一个用于单元测试,一个用于集成测试。

运行

vendor/bin/pest --group=unit

将运行单元测试

   PASS  Tests\Unit\ExampleTest
  ✓ example

  Tests:  1 passed
  Time:   0.02s

然后运行

vendor/bin/pest --group=integration

将运行集成测试

Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.

   PASS  Tests\Integration\ExampleTest
  ✓ Rest API endpoints work
  ✓ Creating terms in category works

  Tests:  2 passed
  Time:   0.14s

测试套件是分组在一起的,如果您想运行集成测试,必须传递 --group=integration 选项,因为这样引导知道在运行测试时加载特定于集成测试的配置。

在CI管道中运行此包

如果您想将此包作为持续集成(CI)管道的一部分运行,请在运行 wp-pest setup 命令时提供 --skip-delete 参数。这将跳过删除 wp-content 文件夹(在CI环境中并不重要),并且不会阻止设置脚本。

问题

为什么PHP版本这么高?如果我想在其他PHP版本上测试我的主题/插件怎么办?

此包的潜在目标是(除了让WordPress开发者更熟悉测试之外)敦促开发者更新他们的项目,并使用更多现代的PHP特性。虽然WordPress支持PHP 5.6,但它在撰写本文时已经不再支持安全补丁(此时PHP 7.4正处于EOL阶段)。

WordPress社区需要前进,如果这个包能帮助某人更新他们的服务器和PHP版本,我会认为这是一个成功。

脚本在下载WordPress部分卡住了,我该怎么办?

它并没有卡住!😂

你可能在WSL上运行它,对吧?由于某种原因,WSL终端上的下载可能很慢。
这是一个已知问题

解决方案可能是禁用一些网络适配器,如此处所述(你还可以阅读一个总结版本 😅)。

它在Windows上不起作用

我还没有在原生Windows安装上测试它。这在我的待办事项列表中,但不是优先事项。

有些东西不起作用

请为此打开一个问题

更新

1.6.0版本

我决定将其名称改为更吸引人的wp-pest。老实说,我不确定为什么之前没有这么做。功能保持不变。

如果你刚刚在1.6.0版本上从头开始下载并设置测试,那么你就准备好了,祝你测试愉快!
如果不是这样,你应该更新你的phpunit.xml文件,以包括

<env name="WP_TESTS_DIR" value="wp/tests/phpunit"/>

在配置的<php>部分。

同时,根据包中的模板更新你的bootstrap.php文件。具体来说,你应该删除文件末尾的行

require_once dirname(__FILE__, 2) . '/wp/tests/phpunit/includes/bootstrap.php';

require_once dirname(__DIR__) . '/vendor/yoast/wp-test-utils/src/WPIntegration/bootstrap-functions.php';

WPIntegration\bootstrap_it();

确保在文件顶部导入bootstrap_it()函数的命名空间

use Yoast\WPTestUtils\WPIntegration;

最后,但同样重要的是,从Pest.php文件中删除Integration

uses(TestCase::class)->in('Unit', 'Integration');

并在每个集成测试的顶部添加

use Yoast\WPTestUtils\WPIntegration\TestCase;

uses(TestCase::class);

这将确保集成测试使用正确的基测试类。