我试图在CakePHP 3.0中运行一些单元测试,但不断出现以下错误,这些错误似乎比我自己构建的问题更多?有人有类似的问题吗?测试代码如下所示:http://book.cakephp.org/3.0/en/development/testing.html
namespace AppTestTestCaseController;
use CakeORMTableRegistry;
use CakeTestSuiteIntegrationTestCase;
/**
* AppControllerUsersController Test Case
*/
class UsersControllerTest extends IntegrationTestCase
{
/**
* Test add method
*
* @return void
*/
public function testAdd()
{
$data = [
'username' => 'testusername',
'password' => 'testpassword',
'email' => 'testemail@test.ie',
'location_id' => 5,
'gender' => 'Male',
'info' => 'test info'
];
$this->post('/users', $data);
$this->assertResponseSuccess();
$users = TableRegistry::get('Users');
$query = $users->find()->where(['username' => $data['username']]);
$this->assertEquals(1, $query->count());
}
响应:
1) AppTestTestCaseControllerUsersControllerTest::testIndex
Use of undefined constant TMP - assumed 'TMP'
C:wampwwwmysitevendorcakephpcakephpsrcNetworkSession.php:141
C:wampwwwmysitevendorcakephpcakephpsrcNetworkSession.php:95
C:wampwwwmysitevendorcakephpcakephpsrcTestSuiteIntegrationTestCas
e.php:340
C:wampwwwmysitevendorcakephpcakephpsrcTestSuiteIntegrationTestCas
e.php:267
C:wampwwwmysitevendorcakephpcakephpsrcTestSuiteIntegrationTestCas
e.php:188
您需要定义CakePHP工作所需的常量。这是在应用程序框架中的tests/bootstrap.php文件中为您完成的:
https://github.com/cakephp/app/blob/master/tests/bootstrap.php#L8
通过包含应用程序引导程序,它还创建并定义了以下常量:
https://github.com/cakephp/app/blob/master/config/paths.php#L61
如果有帮助的话,我也遇到了同样的错误,因为我的项目中缺少phpunit.xml.dist配置文件。
你可以在这里找到它:https://github.com/cakephp/app/blob/master/phpunit.xml.dist.
我也遇到了同样的问题。以下是对我有效的
- 已在全局范围内安装PHPUnit
- 使用composer创建cakephp项目
- 然后使用terminal/CMD goto编写测试项目的根
ex : c:/xampp/htdocs/someproject/
-
然后键入命令
c:/examplep/htdocs/someproject>phpunit
它将运行所有测试。希望这对你有用