我是CodeCeption的新手,我从几个简单的API测试用例开始。我想创建自己的类,以便在多个文件中管理我的方法。我在目录MyClass.php
中编写了一个名为_support
的新PHP文件,其中包含一个名为MyClass
的类。
<?php
class MyClass extends \Codeception\Actor
{
use _generated\ApiTesterActions;
public function SayHi($name) {
$respBody = [
'hi' => 'hi' + $name
];
return json_decode($respBody, true) ;
}
}
我已经在文件codeception.yml
的依赖项中添加了此类:
modules:
enabled:
- MyClass
我还在actor
中将MyClass添加为api.suite.yml
:
actor: MyClass
我试图构建项目以便可以在测试中访问提到的类,但是找不到模块:
zeinab@Zeinab:~/PhpstormProjects/api-testing$ php vendor/bin/codecept build
Building Actor classes for suites: acceptance, api, functional, unit
In ModuleContainer.php line 80:
Module MyClass could not be found and loaded
build
有什么我想念的吗?
我之前已经在文件ApiTester
的类ApiTester.php
中添加了此模块,并且工作正常;我可以构建项目并在测试中使用该方法。
编辑1:
我已根据@Alexey和@Naktibalda帮助说明更改了代码和配置。
这是我的文件结构:
test
|_ src
|_ MyClass.php
|_ tests
|_ ...
|_ mytests
|_ MyCest.php
|_ ...
|_ mytests.suite.yml
|_ ...
|_ vendor
|_ codeception.yml
|_ composer.json
|_ composer.lock
这是我的composer.json
:
{
"require": {
"codeception/base": "^2.5",
"ext-json": "*"
},
"autoload": {
"psr-4": {
"Codeception\\Module\\": "src/"
}
}
}
此代码位于MyClass.php
内:
<?php
class MyClass extends \Codeception\Actor
{
public function SayHi($name) {
$respBody = 'hi' . $name;
return json_decode($respBody, true) ;
}
}
此代码位于MyCest.php
内:
<?php
class MyCest
{
public function MyTestCase(MyClass $I)
{
$I->SayHi('ZiZi');
}
}
这是在mytests.suite.yml
内:
actor: ApiTester
modules:
enabled:
- REST:
depends: PhpBrowser
part: Json
- Asserts:
现在,build正常,其日志表明mytests
已作为套件成功加载:
zeinab@Zeinab:~/PhpstormProjects/test$ codecept build
==== Redirecting to Composer-installed version in vendor/codeception ====
Building Actor classes for suites: acceptance, api, functional, mytests, unit
-> AcceptanceTesterActions.php generated successfully. 0 methods added
\AcceptanceTester includes modules: PhpBrowser, \Helper\Acceptance
-> ApiTesterActions.php generated successfully. 0 methods added
\ApiTester includes modules: \Helper\Api
-> FunctionalTesterActions.php generated successfully. 0 methods added
\FunctionalTester includes modules: \Helper\Functional
-> ApiTesterActions.php generated successfully. 0 methods added
\ApiTester includes modules: REST, PhpBrowser, Asserts
-> UnitTesterActions.php generated successfully. 0 methods added
\UnitTester includes modules: Asserts, \Helper\Unit
但是运行测试遇到错误:
zeinab@Zeinab:~/PhpstormProjects/test$ codecept run tests/mytests/MyCest.php
==== Redirecting to Composer-installed version in vendor/codeception ====
Codeception PHP Testing Framework v2.5.1
Powered by PHPUnit 7.5.0 by Sebastian Bergmann and contributors.
Running with seed:
Mytests Tests (1) -------------------------------------------------------------------------------------------------------------
E MyCest: My test case (0.00s)
-------------------------------------------------------------------------------------------------------------------------------
DEPRECATION: Calling the "Symfony\Component\BrowserKit\Client::getInternalResponse()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0. /home/zeinab/PhpstormProjects/test/vendor/symfony/browser-kit/Client.php:217
Time: 76 ms, Memory: 12.00MB
There was 1 error:
---------
1) MyCest: My test case
Test tests/mytests/MyCest.php:MyTestCase
[InjectionException] Failed to inject dependencies in instance of 'MyCest'. Class MyClass does not exist
#1 phar:///usr/local/bin/codecept/autoload.php:12
#2 /usr/local/bin/codecept:5
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
我认为我在配置composer.json
时仍然遇到问题,但是我无法弄清楚。
编辑2:
我将composer.json
中的类定义更改为``;结果是:
zeinab@Zeinab:~/PhpstormProjects/test$ codecept run tests/mytests/MyCest.php
==== Redirecting to Composer-installed version in vendor/codeception ====
Codeception PHP Testing Framework v2.5.1
Powered by PHPUnit 7.5.1 by Sebastian Bergmann and contributors.
Running with seed:
Mytests Tests (1) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E MyCest: My test case (0.00s)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DEPRECATION: Calling the "Symfony\Component\BrowserKit\Client::getInternalResponse()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0. /home/zeinab/PhpstormProjects/test/vendor/symfony/browser-kit/Client.php:217
Time: 76 ms, Memory: 12.00MB
There was 1 error:
---------
1) MyCest: My test case
Test tests/mytests/MyCest.php:MyTestCase
[InjectionException] Failed to inject dependencies in instance of 'MyCest'. Class MyClass does not exist
#1 phar:///usr/local/bin/codecept/autoload.php:12
#2 /usr/local/bin/codecept:5
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
答案 0 :(得分:1)
我终于解决了这个问题;感谢@Alexey和@Naktibalda。
我将包含我的类MyClass.php
的PHP文件放在目录src/
中,并在composer.json
中将我的类定义为:
{
"require": {
"codeception/base": "^2.5",
"ext-json": "*",
"codeception/codeception": "^2.5"
},
"autoload": {
"classmap": ["src/"]
}
}
这是我的文件结构:
test
|_ src
|_ MyClass.php
|_ tests
|_ ...
|_ mytests
|_ MyCest.php
|_ ...
|_ mytests.suite.yml
|_ ...
|_ vendor
|_ codeception.yml
|_ composer.json
|_ composer.lock
此代码位于MyClass.php
内:
<?php
class MyClass extends \Codeception\Actor
{
public function SayHi($name) {
$respBody = 'hi' . $name;
return $respBody;
}
}
此代码位于MyCest.php
内:
<?php
class MyCest
{
public function MyTestCase(MyClass $I)
{
$resp = $I->SayHi('ZiZi');
echo "Response: " . $resp;
}
}
这是在mytests.suite.yml
内:
actor: ApiTester
modules:
enabled:
- REST:
depends: PhpBrowser
part: Json
- Asserts:
答案 1 :(得分:0)
首先编写一个Helper会更容易吗?在这种情况下,您是从clear: both
而不是\Codeception\Module
扩展您的课程吗?然后将您的配置保持不变,并删除将其添加为Actor