【问题标题】:PHPUnit fails silenltyPHPUnit 静默失败
【发布时间】:2017-01-10 12:14:01
【问题描述】:

当我单独运行$ phpunit 时,它会默默地失败,这就是我试图在没有运气的情况下知道问题所在:

$ phpunit
$ echo $?
=> 255

$ php -i | grep error

display_errors => STDOUT => STDOUT
display_startup_errors => On => On
error_append_string => no value => no value
error_log => /usr/local/var/log/php_error.log => /usr/local/var/log/php_error.log
error_prepend_string => no value => no value
error_reporting => 32767 => 32767
html_errors => Off => Off
ignore_repeated_errors => Off => Off
log_errors => On => On
log_errors_max_len => 1024 => 1024
track_errors => On => On
xmlrpc_error_number => 0 => 0
xmlrpc_errors => Off => Off
xdebug.force_display_errors => On => On
xdebug.force_error_reporting => 1 => 1
xdebug.show_error_trace => Off => Off
opcache.error_log => no value => no value


phpunit --version
PHPUnit 6.0-g969991b by Sebastian Bergmann and contributors.


php --version
PHP 7.0.10-1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.10-1, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

检查语法错误,返回一切正常:

for f in $(find tests/)
> do php -l $f
> done
...

作为临时解决方法,我按如下方式运行所有测试

find . -name "*_Test.php" -type f -exec phpunit {} \;

php_error.log 没有日志条目,并确保文件上没有写入任何内容:

$ truncate -s0 /usr/local/var/log/php_error.log
$ phpunit
$ tail /usr/local/var/log/php_error.log

而且我确实有写入权限来执行日志文件: -rw-rw-r-- 1 adam adam 0 Sep 2 11:49 /usr/local/var/log/php_error.log

为了安全起见:

$ php -r "error_log('am i logged?');"
$ tail /usr/local/var/log/php_error.log
[02-Sep-2016 13:16:37 Europe/Paris] am i logged?

当我这样做时 phpunit tests/* 只有根目录 tests 上的文件正在运行,phpunit 不会递归运行

这是 phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         bootstrap="bootstrap.php"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix=".php">./tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

这是 bootstrap.php 文件的内容

<?php

//
// Author: Oussama Elgoumri
// Email : ktsnepyg9igfz1@gmail.com
//
// Wed Aug 31 17:13:04 WEST 2016
//


require_once __DIR__ . '/vendor/autoload.php';
require_once dirname(dirname(__FILE__)) . '/wp-load.php';
require_once ABSPATH . 'wp-admin/includes/admin.php';


use Dotenv\Dotenv;


do_action('admin_init');

(new Dotenv(__DIR__))
    ->load();

我知道没有错误,这很奇怪,echo $? 运行 phpunit 后返回 255,因为当我运行时

psysh bootstrap.php 可以正常工作

我会很感激任何帮助,即使它是 hacki,我只想运行测试覆盖率并生成报告。 谢谢

更新

作为临时解决方法,我使用以下命令运行所有测试: find . -name "*_Test.php" -type f -exec phpunit {} \;

测试单个文件

$ phpunit tests/Upload__FN_Test.php
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 1.27 seconds, Memory: 24.00MB

OK (1 test, 1 assertion)

测试目录中的所有文件 该目录应该包含子目录,否则它会像上面解释的那样失败并出现 255 错误

$ phpunit tests/src/Uptobox/Workflow_PHP/
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

.....                                                               5 / 5 (100%)

Time: 7.03 seconds, Memory: 26.00MB

OK (5 tests, 6 assertions)

具有测试覆盖率的目录中的测试文件

$ phpunit tests/src/Uptobox/Workflow_PHP/ --coverage-text --whitelist src/Uptobox/Uptobox.php
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

.....                                                               5 / 5 (100%)

Time: 6.93 seconds, Memory: 28.00MB

OK (5 tests, 6 assertions)


Code Coverage Report:    
  2016-09-02 12:35:00    

 Summary:                
  Classes:  0.00% (0/1)  
  Methods: 75.00% (3/4)  
  Lines:   90.91% (10/11)

\App\Uptobox::Uptobox
  Methods:  75.00% ( 3/ 4)   Lines:  90.91% ( 10/ 11)

目前我注意到的是,只要目标目录不包含任何子目录,phpunit 就会处理该目录中的所有测试文件,否则它会静默失败。

【问题讨论】:

  • 你拼错了:&lt;ini name="error_reporting" valaue="true" /&gt; (valaue)。也许这就是原因。
  • 您是否检查了./tests 中的循环符号链接?

标签: php phpunit


【解决方案1】:

您的目录后缀可能是错误的。

这里:您正在尝试执行每个 php 文件作为测试:

<testsuites>
    <testsuite name="Application Test Suite">
        <directory suffix=".php">./tests</directory>
    </testsuite>
</testsuites>

但在这里您将其限制为“*_Test.php”

find . -name "*_Test.php" -type f -exec phpunit {} \;

试试:

<testsuites>
    <testsuite name="Application Test Suite">
        <directory suffix="Test.php">./tests</directory>
    </testsuite>
</testsuites>

【讨论】:

    猜你喜欢
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2017-10-08
    • 2019-07-19
    相关资源
    最近更新 更多