phpunit 未将测试标记为已完成且成功
phpunit not marking test as completed and successful
我在 phpunit
正面测试我创建的方法时遇到问题。我正在使用 zend 应用程序并且 ApplicationTest
工作正常。这是我当前的命令行输出:
ApplicationTest\Controller\IndexController
[x] Sample not active
[x] Index action can be accessed
[x] Index action view model template rendered within layout
[x] Invalid route does not crash
PlutoTest\Stdlib\ArrayUtils
[ ] Sample not active
如您所见,我的 ArrayUtils
测试中没有 X
。我什至把这个方法放在 ApplicationTest
里面,你可以看到它正确执行了。
这是 phpunit class:
namespace PlutoTest\Stdlib;
use Pluto\Stdlib\ArrayUtils;
use Pluto\pluto;
use PHPUnit\Framework\TestCase;
class ArrayUtilsTest extends TestCase
{
public function setUp()
{
$configOverrides = [];
$phpunitConfigFile=$this->getNormalizedPhpunitConfigFile();
$this->setApplicationConfig(ArrayUtils::merge(
include $phpunitConfigFile,
$configOverrides
));
parent::setUp();
}
private function getNormalizedPhpunitConfigFile()
{
$file = sprintf('%s/application.phpunit.php',pluto::path('zfconfig'));
return $file;
}
public function testSampleNotActive()
{
$stack = [];
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
这是我的 phpunit.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
bootstrap="bootstrap.phpunit.php"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
cacheTokens="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestSize="true"
verbose="true">
<testsuites>
<testsuite name="Application Module Test Suite">
<directory phpVersion="7.0.0" phpVersionOperator=">=">./module/Application/test</directory>
<directory phpVersion="7.0.0" phpVersionOperator=">=">./test/pluto/src</directory>
</testsuite>
</testsuites>
<php>
<env name="APPLICATION_ENV" value="development"/>
<ini name="display_errors" value="1"/>
<ini name="display_startup_errors" value="1"/>
<ini name="error_reporting" value="32767"/>
<ini name="report_memleaks" value="1"/>
</php>
</phpunit>
这是我的作曲家:
"autoload-dev" : {
"psr-4" : {
"ApplicationTest\" : "module/Application/test/",
"PlutoTest\" : "test/pluto/src"
}
},
尝试如下更改:
"autoload-dev": {
"psr-4": {
...
"YourModuleNameTest\": "module/YourModuleName/test/"
}
},
如果完成,请不要忘记在您的终端中通过 composer 运行 dump-autoload
。
如果您这样修改 phpunit.xml 会怎样:
<testsuites>
<testsuite name="Application Module Test Suite">
<directory phpVersion="7.0.0" phpVersionOperator=">=">./module/Application/test</directory>
</testsuite>
<testsuite name="YourModuleName">
<directory phpVersion="7.0.0" phpVersionOperator=">=">./module/YourModuleName/test</directory>
</testsuite>
</testsuites>
接下来,运行以下命令:
./vendor/bin/phpunit --testsuite YourModuleName
要列出所有可用套件,运行以下命令:
./vendor/bin/phpunit --list-suites
要列出所有可用的测试,运行以下命令:
./vendor/bin/phpunit --list-tests
我在 phpunit
正面测试我创建的方法时遇到问题。我正在使用 zend 应用程序并且 ApplicationTest
工作正常。这是我当前的命令行输出:
ApplicationTest\Controller\IndexController
[x] Sample not active
[x] Index action can be accessed
[x] Index action view model template rendered within layout
[x] Invalid route does not crash
PlutoTest\Stdlib\ArrayUtils
[ ] Sample not active
如您所见,我的 ArrayUtils
测试中没有 X
。我什至把这个方法放在 ApplicationTest
里面,你可以看到它正确执行了。
这是 phpunit class:
namespace PlutoTest\Stdlib;
use Pluto\Stdlib\ArrayUtils;
use Pluto\pluto;
use PHPUnit\Framework\TestCase;
class ArrayUtilsTest extends TestCase
{
public function setUp()
{
$configOverrides = [];
$phpunitConfigFile=$this->getNormalizedPhpunitConfigFile();
$this->setApplicationConfig(ArrayUtils::merge(
include $phpunitConfigFile,
$configOverrides
));
parent::setUp();
}
private function getNormalizedPhpunitConfigFile()
{
$file = sprintf('%s/application.phpunit.php',pluto::path('zfconfig'));
return $file;
}
public function testSampleNotActive()
{
$stack = [];
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
这是我的 phpunit.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
bootstrap="bootstrap.phpunit.php"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
cacheTokens="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestSize="true"
verbose="true">
<testsuites>
<testsuite name="Application Module Test Suite">
<directory phpVersion="7.0.0" phpVersionOperator=">=">./module/Application/test</directory>
<directory phpVersion="7.0.0" phpVersionOperator=">=">./test/pluto/src</directory>
</testsuite>
</testsuites>
<php>
<env name="APPLICATION_ENV" value="development"/>
<ini name="display_errors" value="1"/>
<ini name="display_startup_errors" value="1"/>
<ini name="error_reporting" value="32767"/>
<ini name="report_memleaks" value="1"/>
</php>
</phpunit>
这是我的作曲家:
"autoload-dev" : {
"psr-4" : {
"ApplicationTest\" : "module/Application/test/",
"PlutoTest\" : "test/pluto/src"
}
},
尝试如下更改:
"autoload-dev": {
"psr-4": {
...
"YourModuleNameTest\": "module/YourModuleName/test/"
}
},
如果完成,请不要忘记在您的终端中通过 composer 运行 dump-autoload
。
如果您这样修改 phpunit.xml 会怎样:
<testsuites>
<testsuite name="Application Module Test Suite">
<directory phpVersion="7.0.0" phpVersionOperator=">=">./module/Application/test</directory>
</testsuite>
<testsuite name="YourModuleName">
<directory phpVersion="7.0.0" phpVersionOperator=">=">./module/YourModuleName/test</directory>
</testsuite>
</testsuites>
接下来,运行以下命令:
./vendor/bin/phpunit --testsuite YourModuleName
要列出所有可用套件,运行以下命令:
./vendor/bin/phpunit --list-suites
要列出所有可用的测试,运行以下命令:
./vendor/bin/phpunit --list-tests