您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit-phpunit.xml-phpunit-testSuiteLoaderClass与testSuiteLoaderFile
发布时间:2021-10-17 19:11:50编辑:雪饮阅读()
testSuiteLoaderClass与testSuiteLoaderFile属性与前面学习到的那个打印类和打印类所在文件差不多的,只是这里是加载测试用例类所用到的类以及所用到的类对应的类文件路径额配置。
testSuiteLoaderClass 属性
默认值:PHPUnit\Runner\StandardTestSuiteLoader
此属性配置的是一个类名,这个类必须实现 PHPUnit\Runner\TestSuiteLoader 接口。会用此类的一个实例对象来加载测试套件。
testSuiteLoaderFile 属性
此属性可用于配置 testSuiteLoaderClass 所配置的类的声明所在源代码文件,以防此类无法自动加载的情况。
那么所以我们可以参考(复制)PHPUnit\Runner\StandardTestSuiteLoader来实现自己的加载类.
phpunit.xml:
<phpunit bootstrap="src/autoload.php" testSuiteLoaderClass="MySuiteLoader" testSuiteLoaderFile="/usr/local/organizing/tests/MySuiteLoader.php">
</phpunit>
MySuiteLoader.php:
<?php
declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use function array_diff;
use function array_values;
use function basename;
use function class_exists;
use function get_declared_classes;
use function sprintf;
use function stripos;
use function strlen;
use function substr;
use PHPUnit\Framework\TestCase;
use PHPUnit\Util\FileLoader;
use ReflectionClass;
use ReflectionException;
use PHPUnit\Runner\TestSuiteLoader;
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*
* @deprecated see https://github.com/sebastianbergmann/phpunit/issues/4039
*/
final class MySuiteLoader implements TestSuiteLoader{
/**
* @throws Exception
*/
public function load(string $suiteClassFile) : ReflectionClass
{
echo "\r\n我来了\r\n";
$suiteClassName = basename($suiteClassFile, '.php');
$loadedClasses = get_declared_classes();
if (!class_exists($suiteClassName, \false)) {
/* @noinspection UnusedFunctionResultInspection */
FileLoader::checkAndLoad($suiteClassFile);
$loadedClasses = array_values(array_diff(get_declared_classes(), $loadedClasses));
if (empty($loadedClasses)) {
throw $this->exceptionFor($suiteClassName, $suiteClassFile);
}
}
if (!class_exists($suiteClassName, \false)) {
// this block will handle namespaced classes
$offset = 0 - strlen($suiteClassName);
foreach ($loadedClasses as $loadedClass) {
if (stripos(substr($loadedClass, $offset - 1), '\\' . $suiteClassName) === 0) {
$suiteClassName = $loadedClass;
break;
}
}
}
if (!class_exists($suiteClassName, \false)) {
throw $this->exceptionFor($suiteClassName, $suiteClassFile);
}
try {
$class = new ReflectionClass($suiteClassName);
// @codeCoverageIgnoreStart
} catch (ReflectionException $e) {
throw new \PHPUnit\Runner\Exception($e->getMessage(), (int) $e->getCode(), $e);
}
// @codeCoverageIgnoreEnd
if ($class->isSubclassOf(TestCase::class) && !$class->isAbstract()) {
return $class;
}
if ($class->hasMethod('suite')) {
try {
$method = $class->getMethod('suite');
// @codeCoverageIgnoreStart
} catch (ReflectionException $e) {
throw new \PHPUnit\Runner\Exception($e->getMessage(), (int) $e->getCode(), $e);
}
// @codeCoverageIgnoreEnd
if (!$method->isAbstract() && $method->isPublic() && $method->isStatic()) {
return $class;
}
}
throw $this->exceptionFor($suiteClassName, $suiteClassFile);
}
public function reload(ReflectionClass $aClass) : ReflectionClass
{
return $aClass;
}
private function exceptionFor(string $className, string $filename) : \PHPUnit\Runner\Exception
{
return new \PHPUnit\Runner\Exception(sprintf("Class '%s' could not be found in '%s'.", $className, $filename));
}
}
?>
然后我们再提高一个测试程序:
MyTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
public function testOne(): void
{
$this->assertTrue(true);
}
public function testTwo(): void
{
$this->assertTrue(true);
}
public function testThree(): void
{
$this->assertTrue(true);
}
}
那么运行测试之结果:
[root@localhost organizing]# /usr/local/php734/bin/php /usr/local/phpunit-9.5.8.phar -c /usr/local/organizing/phpunit.xml /usr/local/organizing/tests/MyTest.php
Warning: The use statement with non-compound name 'array_diff' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 13
Warning: The use statement with non-compound name 'array_values' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 14
Warning: The use statement with non-compound name 'basename' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 15
Warning: The use statement with non-compound name 'class_exists' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 16
Warning: The use statement with non-compound name 'get_declared_classes' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 17
Warning: The use statement with non-compound name 'sprintf' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 18
Warning: The use statement with non-compound name 'stripos' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 19
Warning: The use statement with non-compound name 'strlen' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 20
Warning: The use statement with non-compound name 'substr' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 21
Warning: The use statement with non-compound name 'ReflectionClass' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 24
Warning: The use statement with non-compound name 'ReflectionException' has no effect in /usr/local/organizing/tests/MySuiteLoader.php on line 25
我来了
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
Warning: Using a custom test suite loader is deprecated
... 3 / 3 (100%)
Time: 00:00, Memory: 18.00 MB
OK (3 tests, 3 assertions)
关键字词:phpunit,testSuiteLoaderFile,testSuiteLoaderClass
相关文章
- phpunit-phpunit.xml-phpunit-timeoutForLargeTests
- phpunit-phpunit.xml-phpunit-timeoutForMediumTests
- phpunit-phpunit.xml-phpunit-timeoutForSmallTests
- phpunit-phpunit.xml-phpunit的enforceTimeLimit与def
- phpunit-phpunit.xml-phpunit-beStrictAboutTodoAnnot
- phpunit-phpunit.xml-beStrictAboutTestsThatDoNotTes
- phpunit-phpunit.xml-phpunit-beStrictAboutOutputDur
- phpunit-phpunit.xml-phpunit-stopOnDefect
- phpunit-phpunit.xml-stopOnSkipped
- phpunit-phpunit.xml-phpunit-stopOnRisky