所以这太烦人了。我有一个测试文件,其中包含一个测试函数和一些断言。我可以证明,当我在命令行中运行phpunit
运行测试函数时(通过写入日志)但没有任何断言通过或失败。当断言应该运行但似乎没有出现在输出或我的/var/log/nginx/error.log
文件中时,似乎存在PHP错误。 我需要做些什么来让我的断言运行?
这是我运行phpunit
时的输出:
PHPUnit 3.7.38 by Sebastian Bergmann.
Configuration read from <PATH_TO_TEST_FOLDER>/test/phpunit.xml
hello1
请注意,您没有看到here之类的摘要。
这是<PATH_TO_TEST_FOLDER>/test/ApplicationTest/Controller/IndexControllerTest.php
文件:
<?php
namespace ApplicationTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class IndexControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
protected function setUp()
{
$dir = getcwd();
$this->setApplicationConfig(
include $dir . '/config/application.config.php'
);
parent::setUp();
}
public function testIndexActionCanBeAccessed()
{
error_log('hello1');
$this->assertEquals(true, false);
error_log('hello2');
}
}
这是我的<PATH_TO_TEST_FOLDER>/test/Bootstrap.php
文件:
<?php
namespace ApplicationTest;
use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use RuntimeException;
error_reporting( E_ALL | E_STRICT );
chdir( __DIR__ );
class Bootstrap {
protected static $serviceManager;
public static function init() {
// Load the user-defined test configuration file, if it exists; otherwise, load
if ( is_readable( __DIR__ . '/TestConfig.php' ) ) {
$testConfig = include __DIR__ . '/TestConfig.php';
} else {
$testConfig = include __DIR__ . '/TestConfig.php.dist';
}
$zf2ModulePaths = array ();
if ( isset( $testConfig['module_listener_options']['module_paths'] ) ) {
$modulePaths = $testConfig['module_listener_options']['module_paths'];
foreach ( $modulePaths as $modulePath ) {
if ( ($path = static::findParentPath( $modulePath )) ) {
$zf2ModulePaths[] = $path;
}
}
}
static::initAutoloader();
// use ModuleManager to load this module and it's dependencies
$config = array (
'module_listener_options' => array (
'module_paths' => $zf2ModulePaths
),
'modules' => array (
'Application'
)
);
$serviceManager = new ServiceManager( new ServiceManagerConfig() );
$serviceManager->setService( 'ApplicationConfig', $config );
$serviceManager->get( 'ModuleManager' )->loadModules();
static::$serviceManager = $serviceManager;
}
public static function chroot() {
$rootPath = dirname( static::findParentPath( 'module' ) );
chdir( $rootPath );
}
public static function getServiceManager() {
return static::$serviceManager;
}
protected static function initAutoloader() {
$vendorPath = static::findParentPath('vendor');
$zf2Path = getenv('ZF2_PATH');
if (!$zf2Path) {
if (defined('ZF2_PATH')) {
$zf2Path = ZF2_PATH;
} elseif (is_dir($vendorPath . '/ZF2/library')) {
$zf2Path = $vendorPath . '/ZF2/library';
} elseif (is_dir($vendorPath . '/zendframework/zendframework/library')) {
$zf2Path = $vendorPath . '/zendframework/zendframework/library';
}
}
if (!$zf2Path) {
throw new RuntimeException(
'Unable to load ZF2. Run `php composer.phar install` or'
. ' define a ZF2_PATH environment variable.'
);
}
if (file_exists($vendorPath . '/autoload.php')) {
include $vendorPath . '/autoload.php';
}
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
),
),
));
}
protected static function findParentPath( $path ) {
$previousDir = '.';
while ( !is_dir( $dir . '/' . $path ) ) {
$dir = dirname( $dir );
if ( $previousDir === $dir ) {
return false;
}
$previousDir = $dir;
}
return $dir . '/' . $path;
}
}
Bootstrap::init();
Bootstrap::chroot();
这是我的<PATH_TO_TEST_FOLDER>/test/phpunit.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php" colors="true">
<testsuites>
<testsuite name="application">
<directory>./ApplicationTest</directory>
</testsuite>
</testsuites>
</phpunit>