PHPUnit不会自动检测目录中的文件
因此,我尝试在WordPress中创建一些单元测试,并安装了PHPUnit 6.5.5(尝试了7.*之前的各种版本,这是WP支持的最新版本) 生成的phpunit.xml.dist文件如下所示:PHPUnit不会自动检测目录中的文件,php,unit-testing,phpunit,Php,Unit Testing,Phpunit,因此,我尝试在WordPress中创建一些单元测试,并安装了PHPUnit 6.5.5(尝试了7.*之前的各种版本,这是WP支持的最新版本) 生成的phpunit.xml.dist文件如下所示: <?xml version="1.0"?> <phpunit bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="t
<?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>
然而,当我在插件目录中运行phpunit
时,我得到没有执行测试
当我将测试文件添加到目录下时:
<file>tests/test-sample.php</file>
编辑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不带命令行选项
在没有命令行选项的情况下运行phpunit
时,您应该想知道当前的工作目录是什么,如中所示。
如果当前工作目录中存在phpunit.xml或phpunit.xml.dist(按该顺序),并且未使用--configuration,则将自动从该文件读取配置
- 带有命令行选项的phpunit
如果要从不包含phpunit.xml的目录(例如插件的目录)运行phpunit,则必须传递正确的路径:
phpunit--configuration/vagrant/Plugins/Framework/phpunit.xml.dist
当您在phpunit.xml.dist中配置bootstrap=“tests/bootstrap.php”
时,phpunit将在/vagrant/Plugins/Framework/tests
目录中查找bootstrap.php
文件
bootstrap.php文件包含以下行:
$\u tests\u dir=getenv('WP\u tests\u dir');
如果(!$\u tests\u dir)$\u tests\u dir='/tmp/wordpress tests lib'代码>
我建议您将其替换为:
$\u tests\u dir='/tmp/wordpress tests lib'代码>
您还可以在~/.bash_配置文件中添加此选项,以避免重复替换:
export-WP\u-TESTS\u-DIR='/tmp/wordpress-TESTS-lib'
配置看起来正常,必须与环境或phpunit本身有关。什么返回bin/phpunit-v?另外,请尝试删除前导“/”字符。不知怎的,它开始工作了。这对我来说也是个谜。phpunit-v在php版本中显示为额外版本(7.30以及正确路径的phpunit.xml.dist。因此,我正在使用vagrant,每次使用vagrant时,我都会遇到以下错误:找不到/tmp/wordpress tests lib/includes/functions.php,您运行过bin/install-wp-tests.sh吗?
我需要运行bin/install-wp-tests.sh wordpress\u test root''localhost latest
ea吗运行此命令后,我的单元测试不会在VM中运行(未找到测试)。在我的主机上,它们似乎加载正确。您提到了phpunit.xml.dist
,而phpunit-v
显示的路径是/vagrant/Plugins/Framework/phpunit.xml
。后者的内容是什么?我重命名了文件,然后将其切换回.dist。我执行的phpunit
文件夹来自/vagrant>/Plugins/Framework/
它选择了正确的XML。使用的配置文件是正确的。我可以逐个手动运行测试。似乎phpunit无法找到测试文件并自动运行它们。你的bootstrap.php是什么样子的?我在帖子中添加了bootstrap.php内容。这基本上就是wp scaffold plugin测试生成。我只更改了插件的路径,这应该不会有任何影响,因为它基本上是相同的路径。您的$WP_CORE_DIR环境变量中有什么?不确定我是否正确执行了此操作,但在bootstrap.php中添加了var_dump(getenv('WP_CORE_DIR');
bool(false)
。尝试设置$\u tests\u dir(如您所述)并编辑bash\u配置文件,但仍然没有运行任何测试。
$ 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
<?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';