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="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

您应该想知道当您在没有命令行选项的情况下运行 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 将在 中查找 bootstrap.php 文件/vagrant/Plugins/Framework/tests 目录。

您的 bootstrap.php 文件包含这些行:

$_tests_dir = getenv( 'WP_TESTS_DIR' ); 如果 (!$_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 不会自动检测目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60717588/

相关文章:

php - 在 Woocommerce 结帐注册中用计费名字替换用户名

javascript - 无符号右移函数不适用于负输入

objective-c - Objective C & OC Mock - 模拟类方法?

单元测试 PostgreSQL 行级锁

javascript - 如何在 Chrome、Microsoft Edge 或任何其他浏览器中自动化 UI 测试?

Phpunit,一个测试 - 一种方法?

symfony - 使用基本 http 身份验证的 REST 单元测试始终为 401 Unauthorized

laravel - Lumen Artisan 命令在测试时抛出 "Error: Call to a member function assertExitCode() on int"

php - 纵向宽度对轴承有何影响

php - 应用服务层——如何编写API方法接口(interface)