我正在编写一个控制模块,但是我找不到可以运行测试单元和功能的文档。我知道我应该使用PhpUnit,但我找不到PhpUnit配置文件的示例,更重要的是,没有关于如何执行功能测试的资源--即对运行的Drupal实例进行模块测试。
我一直在研究一些著名的控制模块,比如段落,在它们的文档中没有任何关于运行测试的内容--而且根本不依赖phpunit。这对我来说毫无意义,因为有些功能测试已经实现。
我们应该如何为控制模块提供测试呢?
发布于 2019-07-11 06:40:13
您必须在composer中包含phpunit/phpunit包,如果您使用默认drupal安装,则几乎可以这样做:
composer require phpunit/phpunit --dev应该填充描述测试套件(通常是phpunit.xml )的文件。
它看起来像这样的./phpunit.xml:
<phpunit bootstrap="web/core/tests/bootstrap.php" colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
checkForUnintentionallyCoveredCode="false"
printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter">
<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"/>
</php>
<testsuites>
<testsuite name="testname1">
<directory>web/modules/custom/your_module/tests</directory>
</testsuite>
<testsuite name="testname2">
<directory>web/modules/custom/your_module2/tests</directory>
</testsuite>
</testsuites>
</phpunit>在本例中,drupal位于./web/目录中,./vendor/位于根路径(./)中。
您可以为cont肋骨模块添加测试,但是当作者将源代码推到drupal.org ( https://www.drupal.org/pift-ci-job/1339317 )时,它们已经进行了测试(如果它们包含测试):
<testsuite name="toolbar">
<directory>./web/modules/contrib/admin_toolbar/tests/src/FunctionalJavascript</directory>
</testsuite>然后,您可以使用以下方法运行测试:
./vendor/phpunit/phpunit/phpunit若要使用特定描述文件,请执行以下操作:
./vendor/phpunit/phpunit/phpunit -c phpunit-other.xml要运行特定的测试套件:
./vendor/phpunit/phpunit/phpunit --testsuite=testname1https://drupal.stackexchange.com/questions/283328
复制相似问题