2

我是 PHP 和 Codeception 以及单元测试的整个概念的新手。到目前为止,我已经遵循 Codeception 的快速入门指南 ( http://codeception.com/quickstart ) 并阅读了他们的文档 ( http://codeception.com/docs/05-UnitTests )。

我已经设法设置了测试环境,并且

  1. 创建单元测试文件 ( php codecept.phar generate:test unit ExampleTest)
  2. 运行测试命令 ( php codecept.phar run unit ExampleTest),它返回错误:

有 1 个错误:

1) ExampleTest: Validation
 Test  tests\unit\ExampleTest.php:testValidation

 [Error] Class 'User' not found  

#1  ExampleTest->testValidation
#2  C:\laragon\www\kario\vendor\bin\codecept.phar:5

我不明白的是测试文件如何知道要在哪个 PHP 文件上运行测试?

我的 laragon 项目名为kario,并且在C:\laragon\www\kario\resources\views\pages\orders测试单元文件位于C:\laragon\www\kario\vendor\bin\tests\unit.

4

1 回答 1

0

我也有这个问题,并为自己找到了答案。在这里发布http://phptest.club/t/beginner-codeception-unit-test-help/1849 而且,你去:

首先,一些细节。我正在使用由 PHPUnit 7.1.4 提供支持的 Codeception v2.4.1。答案是:

codeception.yml中,添加以下两行:

    settings:
        bootstrap: _bootstrap.php

这里_bootstrap.php可以是您希望引导文件的名称。

您必须放置_bootstrap.php在以下每个目录中,如下所示:

tests/unit/_bootstrap.php
tests/functional/_bootstrap.pp
tests/acceptance/_bootstrap.php

在我的tests/unit/_bootstrap.php文件中,我放置了以下代码:

<?php
use Codeception\Util\Autoload;
Autoload::addNamespace('myclassnamespace', __DIR__ . '/../../Classes/');

为了确保我有正确的 Classes 路径,我在添加该行之前使用trigger_error(__DIR__)了我的。_bootstrap.phpAutoload

然后在我的 中tests/unit/TestAddCest.php,我将以下行放在文件的开头:

<?php
use mynamespace;

在我的测试函数中看起来像这样(注意 User 类的实例化):

    public function tryToTest(UnitTester $I)
    {
      $user = new mynamespace\User('someusername');
      $I->assertEquals('someusername', $user->username);
    }

我手动输入了该函数,因为我与代码不在同一台机器上并且不想完成它,所以它可能有错字或错误,但你明白了。

编辑 05/01/2018:其他人在http://phptest.club上回答了我:

最好配置类的自动加载composer.json并让 Composer 完成其余的工作,除非您尝试避免使用 Composer。

https://getcomposer.org/doc/01-basic-usage.md#autoloading

于 2018-04-28T16:08:05.823 回答