【发布时间】:2020-03-17 07:00:43
【问题描述】:
所以我尝试在 WordPress 中创建一些单元测试,并安装了 PHPUnit 6.5.5(尝试了 7.* 的各种版本,这是 WP 支持的最新版本)。
生成的 phpunit.xml.dist 文件如下所示:
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="default">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
tests 文件夹是默认文件夹,包含 bootstrap.php 和 test-sample.php 文件:
tests
-- bootstrap.php
-- test-sample.php
但是,当我在插件目录中运行 phpunit 时,我得到 没有执行任何测试。
当我在目录下添加测试文件为:
<file>tests/test-sample.php</file>
然后运行phpunit,可以看到测试运行了。
不应该自动检测测试套件中的目录和文件,而不需要将每个测试文件一一写入XML吗?
编辑: phpunit -v 输出:
$ phpunit -v
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.
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.14-1+ubuntu18.04.1+deb.sury.org+1
Configuration: /vagrant/Plugins/Framework/phpunit.xml.dist
Time: 1 second, Memory: 26.00MB
编辑 2: bootstrap.php 文件内容:
<?php
/**
* PHPUnit bootstrap file
*/
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit( 1 );
}
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require realpath(dirname(__FILE__) . '/../..') . '/myplugin/myplugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
【问题讨论】:
-
配置看起来没问题,肯定有环境或phpunit本身的东西。什么返回 bin/phpunit -v ?另外请尝试删除前导“./”字符。
-
不知何故,它开始工作了。这对我来说也是一个谜。 phpunit -v 显示额外的 php 版本(7.30 以及正确路径的 phpunit.xml.dist。
-
所以我正在使用 vagrant 并且在每次 vagrant up 时都会出现以下错误:
Could not find /tmp/wordpress-tests-lib/includes/functions.php, have you run bin/install-wp-tests.sh ?每次我 vagrant up 时都需要运行bin/install-wp-tests.sh wordpress_test root '' localhost latest吗?运行此之后,我的单元测试不在 VM 中运行(未找到测试)。在我的主机上,它们似乎可以正确加载。 -
你提到了
phpunit.xml.dist,而phpunit -v显示的路径是/vagrant/Plugins/Framework/phpunit.xml。后者的内容是什么? -
我重命名了文件,但随后又将其切换回 .dist。我执行
phpunit的文件夹来自/vagrant/Plugins/Framework/,它选择了正确的XML。
标签: php unit-testing phpunit