【问题标题】:How to run PHPUnit tests in a Drupal 8 environment for custom modules如何在 Drupal 8 环境中为自定义模块运行 PHPUnit 测试
【发布时间】: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

【问题讨论】:

    标签: phpunit drupal-8


    【解决方案1】:

    正如我所见,@kamal,问题是您正在将测试实例化为:使用 PHPUnit\Framework\TestCase;但是你使用的是 Drupal,所以应该如下:use Drupal\Tests\UnitTestCase;

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2018-11-23
      • 2023-02-19
      • 1970-01-01
      相关资源
      最近更新 更多