问题描述
我是 PHP 和 codecept 的新手,一直在学习教程,但是当我尝试运行一个简单的单元测试时,我没有得到任何测试结果。
我的项目结构如下:
类的代码是:
<?PHP
//Have we already been here?
if (!defined('DB_HOST')) {
// Define DB Params
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PASS","");
define("DB_NAME","philter");
// Define URL
define("ROOT_URL","http://bit703.module2/");
// Add some helpful filepath constants
define('APP_ROOT',dirname(realpath(__FILE__)) . '/');
define('VIEW_ROOT',APP_ROOT . 'Views/');
// Tell this sytem we are in a development environment
define('DEV_ENV',true);
}
Utils.PHP
<?PHP
/*
* spl_autoload_register() allows you to register multiple functions
* (or static methods from your own Autoload class)
* that PHP will put into a stack/queue and call sequentially
* when a "new Class" is declared.
*/
spl_autoload_register(function ($class) {
/*
* Project-specific namespace prefix
*/
$prefix = 'BIT703\\';
/*
* Does the class use the namespace prefix?
*/
$len = strlen($prefix);
if (strncmp($prefix,$class,$len) !== 0) {
// no,move to the next registered autoloader
return;
}
/*
* Get the relative class name
*/
$relative_class = substr($class,$len);
print("<pre>".print_r($relative_class,1)."</pre>");
/*
* Replace the namespace prefix with the base directory,replace namespace
* separators with directory separators in the relative class name,append
* with .PHP
*/
$file = APP_ROOT . str_replace('\\','/',$relative_class) . '.PHP';
print("<pre>".print_r($file,1)."</pre>");
die();
/*
* if the file exists,require it
*/
if (file_exists($file)) {
require $file;
}
});
$router = new \BIT703\Classes\Router();
Bootstrap.PHP
<?PHP
require 'config.PHP';
require 'utils.PHP';
RouterTest.PHP
<?PHP
require(dirname(__FILE__).'/../../src/App/bootstrap.PHP');
use \BIT703\Classes\Router as Router;
class RouterTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
*/
protected $tester;
protected function _before()
{
}
protected function _after()
{
}
// tests
public function testSomeFeature()
{
$this->assertInstanceOf('\BIT703\Classes\Router',$this->router);
}
}
这是我运行测试时得到的输出:
$ PHP vendor/bin/codecept run unit RouterTest
Codeception PHP Testing Framework v4.1.18
Powered by PHPUnit 9.5.4 by Sebastian Bergmann and contributors.
<pre>Classes\Router</pre>.
<pre>/Applications/MAMP/htdocs/bit703/module2/src/App/Classes/Router.PHP</pre>
如果我注释掉“require 'utils.PHP';”从 bootstrap.utils,然后我收到一条正确的测试失败消息,但是一旦包含它,它就会停止工作。
谁能看出我错在哪里?
解决方法
我看到您创建了默认的 Codeception 目录结构,其中包含单元、功能和验收目录。
测试文件必须放在这些目录之一,所以将 RouterTest.php 文件移动到单元目录。