【发布时间】:2019-08-14 16:17:33
【问题描述】:
我正在尝试为 Drupal 8 创建和运行 PHPUnit 测试。以下是详细信息:
我的顶级目录,composer.json 所在的位置。
Dockerfile bootstrap.php composer.lock phpunit-examples phpunit.xml.org web
Jenkinsfile checkstyle.xml config phpunit.xml scripts
LICENSE components drush phpunit.xml.dist sonar-project.properties
README.md composer.json patches phpunit.xml.dist.org vendor
./vendor/bin/phpunit --version Sebastian Bergmann 和贡献者的 PHPUnit 6.5.14。
phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
verbose="true"
>
<testsuites>
<testsuite name="unit">
<file>./tests/TestSuites/UnitTestSuite.php</file>
</testsuite>
<testsuite name="kernel">
<file>./tests/TestSuites/KernelTestSuite.php</file>
</testsuite>
<testsuite name="functional">
<file>./tests/TestSuites/FunctionalTestSuite.php</file>
</testsuite>
<testsuite name="functional-javascript">
<file>./tests/TestSuites/FunctionalJavascriptTestSuite.php</file>
</testsuite>
</testsuites>
</phpunit>
phpunit.xml
<phpunit
bootstrap="bootstrap.php"
colors="true"
strict="true"
verbose="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
checkForUnintentionallyCoveredCode="false">
<testsuites>
<testsuit name="Simple Example Test Suite">
<directory>phpunit-examples/tests</directory>
</testsuit>
</testsuites>
<php>
<ini name="error_reporting" value="32767"/>
<ini name="memory_limit" value="-1"/>
<env name="SIMPLETEST_BASE_URL" value="http://drupal-8.localhost"/>
<env name="SIMPLETEST_DB" value="mysql://drupal-8:drupal-8@localhost/drupal-8"/>
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/var/www/sites/default/simpletest"/>
<includePath>phpunit-examples/src/</includePath>
</php>
</phpunit>
要测试的自定义代码: web/modules/custom/benefit/src/BenefitListBuilder.php
测试位于: web/modules/custom/benefit/tests/src/BenefitListBuilderTest.php
<?php declare(strict_types = 1);
namespace Drupal\Tests\benefit;
use Mockery;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use Drupal\benefit\BenefitListBuilder;
/**
* Test basic functionality of My Module.
*
* @group benefit
*/
class BenefitListBuilderTest extends UnitTestCase
{
/** @var BenefitListBuilder */
private $benefitListBuilder;
protected function setUp()
{
$a = "var_a";
$b = "var_b";
$this->benefitListBuilder = new BenefitListBuilder($a,$b);
}
public function testMissing()
{
$this->fail('Test not yet implemented');
}
}
现在,我尝试运行这个测试:
$./vendor/bin/phpunit web/modules/custom/benefit/tests/src/BenefitListBuilderTest.php
PHP Fatal error: Class 'Drupal\Tests\benefit\UnitTestCase' not found in /Users/syedahmed/BG-REPOS/PHPUNITTEST-BenefitsAPI/BenefitsAPI/web/modules/custom/benefit/tests/src/BenefitListBuilderTest.php on line 15
Fatal error: Class 'Drupal\Tests\benefit\UnitTestCase' not found in /Users/syedahmed/BG-REPOS/PHPUNITTEST-BenefitsAPI/BenefitsAPI/web/modules/custom/benefit/tests/src/BenefitListBuilderTest.php on line 15
我尝试在 web/modules/custom/benefit/tests/src/Unit/BenefitListBuilderTest.php 下移动测试,但得到了同样的错误。 如何让测试识别 UnitTestCase 的路径?
更新: 我已经在 PHPStorm 中设置了存储库,所以现在出现错误:
Error : Class 'Drupal\Tests\BenefitListBuilder' not found
【问题讨论】: