Codeception 的 Autoload 类不会自动加载

Codeception's Autoload class doesn't autoload

提问人:grazdev 提问时间:4/22/2022 更新时间:4/22/2022 访问量:365

问:

我正在尝试将一堆助手自动加载到测试套件中。这些帮助程序位于测试套件外部的文件夹中,希望我可以在需要时在多个项目中重用它们。

这几乎就是我所拥有的:

- helpers
    - TestHelper.php
- tests
    - _data
    - _output
    - _support
        - _generated
        - Helper
            - Integration.php
        - IntegrationTester.php
    - integration
    - bootstrap.php
    - integration.suite.yml
- vendor
- codeception.yml

这是引导文件

// bootstrap.php

<?php

\Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "../helpers" );

这是全局配置文件:

// codeception.yml

bootstrap: bootstrap.php
namespace: main
paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed

这是集成套件的配置文件:

// integration.suite.yml

actor: IntegrationTester
modules:
    enabled:
        - \awesome\helpers\TestHelper
        - \main\Helper\Integration

这是 TestHelper:

<?php

namespace awesome\helpers;

class TestHelper extends \Codeception\Module{

   public function sayHello() {
      return "Hello"; 
   }

}

一旦我这样做,我就收到以下错误:codecept run

找不到并加载模块 \awesome\helpers\TestHelper

我没有发布任何测试,因为它无关紧要,在执行任何测试之前都会引发错误,因为它是配置问题。

据我了解,全局配置文件应该在运行测试之前运行引导文件,并且引导文件中的 Autoload 类应该在命名空间中加载帮助程序,但这显然没有发生。我做错了什么?awesome\helpers

PHP 测试 命名空间 自动加载 Codeception

评论

0赞 Naktibalda 4/22/2022
只需改用 Composer Autoloader 即可。

答:

1赞 Naktibalda 4/22/2022 #1

我认为你犯了一个经典的错误,之前错过了,所以你把路径设定为 ./..tests../helpers

改变

\Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "../helpers" );

\Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "/../helpers" );

评论

0赞 grazdev 4/22/2022
啊,是的!谢谢。