【问题标题】:PHPUnit does not automatically detect files in directoryPHPUnit 不会自动检测目录中的文件
【发布时间】: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


【解决方案1】:
  • 没有命令行选项的phpunit

当您运行phpunit 时不带命令行选项时,您应该想知道您当前的工作目录是什么,正如您在phpunit 6.5 doc 中看到的那样。 如果当前工作目录中存在 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 文件包含以下几行:

$_tests_dir = getenv( 'WP_TESTS_DIR' ); if ( !$_tests_dir ) $_tests_dir = '/tmp/wordpress-tests-lib';

我建议您将它们替换为: $_tests_dir = '/tmp/wordpress-tests-lib';

您也可以在 ~/.bash_profile 中添加它以避免重复替换:

export WP_TESTS_DIR='/tmp/wordpress-tests-lib'

【讨论】:

  • 使用的配置文件是正确的。我可以一个一个地手动运行测试。似乎phpunit找不到测试文件并自动运行它们。
  • 你的 bootstrap.php 是什么样的?
  • 我已在我的帖子中添加了 bootstrap.php 内容。它基本上是wp scaffold plugin-tests 生成的。我只更改了插件的路径,它应该不会有任何影响,因为它基本上是相同的路径。
  • 你的 $WP_CORE_DIR 环境变量中有什么?
  • 不确定我是否正确执行此操作,但在 bootstrap.php 中添加 var_dump(getenv( 'WP_CORE_DIR' )); 会返回 bool(false)。尝试按照您提到的设置 $_tests_dir 以及编辑 bash_profile 但仍然没有运行测试。
猜你喜欢
  • 2013-11-08
  • 1970-01-01
  • 2016-04-16
  • 2017-06-11
  • 1970-01-01
  • 2019-08-10
  • 2010-09-09
  • 1970-01-01
  • 2018-08-10
相关资源
最近更新 更多