【问题标题】:PHPUnit Test Cases not executingPHPUnit 测试用例未执行
【发布时间】:2015-06-15 23:10:05
【问题描述】:

通过加入测试驱动开发的潮流,试图成为一名优秀的后端开发人员 我刚刚通过 composer 安装了 PHPUnit。然后我运行命令 vendor/bin/phpunit 并且可以看到 PHPUnit 已正确安装。

下面是我的文件结构:

-fileserver
  src
  tests/
  vendor
  composer.json
  composer.lock
  index.php
  phpunit.xml

在 phpunit.xml 文件中我有以下内容:

<?xml version="1.0" encoding="UTF-8"?>
  <phpunit colors="true">
    <testsuites>
      <testsuite name="File Server Test Suite">
        <directory>./fileserver/tests/</directory>
      </testsuite>
    </testsuites>
  </phpunit>

如果我运行 vendor/bin/phpunit 我可以看到我的配置文件已经加载但没有运行测试。

我基本上在测试文件夹中创建了一个简单的测试文件并扩展了 PHPUnit_Framework_TestCase。我所有的测试都使用 psr-4 命名空间 PhpUnitTest。

composer.json

//Other part excluded
"autoload": {
  "psr-4": { 
    "FileServer\\": "src" ,
    "PhpUnitTest\\": "tests"
  }

示例测试类

namespace PhpUnitTest;

 class StupidTest extends \PHPUnit_Framework_TestCase
 {
   public function testTrueIsTrue()
   {
     $foo = true;
     $this->assertTrue($foo);
   }
 }

但是下面是我得到的输出:

PS C:\wamp\www\fileserver> vendor/bin/phpunit
PHPUnit 3.7.14 by Sebastian Bergmann.

Configuration read from C:\wamp\www\fileserver\phpunit.xml



  Time: 3.53 seconds, Memory: 1.25Mb

←[30;43m←[2KNo tests executed!
←[0m←[2KPS C:\wamp\www\fileserver>

我错过了什么?为什么我的测试用例没有被执行?提前谢谢你。

【问题讨论】:

    标签: php phpunit


    【解决方案1】:

    您没有将 composer 生成的自动加载器添加到您的 phpunit.xml 文件中。在phpunit 标签上设置bootstrap 属性。您的测试套件的路径也不正确。路径应该是相对于phpunit.xml 文件的,所以你应该在路径中去掉fileserver/

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit bootstrap="./vendor/autoload.php" colors="true">
        <testsuites>
            <testsuite name="File Server Test Suite">
                <directory>tests/</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    

    应该这样做

    【讨论】:

    • 谢谢。但是,我按照您的指示做了,但仍然看不到任何测试结果.... :-(
    • @LuyandaSiko:没有发现另一个问题:你的测试套件的路径也是错误的
    • 谢谢埃利亚斯。显然我遵循的教程已经过时......我的测试成功运行...... :-)
    猜你喜欢
    • 2018-05-05
    • 2018-10-08
    • 2014-05-15
    • 2013-07-17
    • 1970-01-01
    • 2014-07-21
    • 2015-05-31
    • 1970-01-01
    • 2013-08-17
    相关资源
    最近更新 更多