Drupal 8用PHPUnit 补充了基于Simpletest的测试框架,Simpletest可以在Drupal 9中删除。
我还没有升级到Drupal 8,但是我想知道如何在PHPUnit中为Drupal 7编写现有测试(以跟上趋势),而不是在Simpletest中编写?
有没有将PHPUnit与Drupal 7集成的方法或模块?
Drupal 8用PHPUnit 补充了基于Simpletest的测试框架,Simpletest可以在Drupal 9中删除。
我还没有升级到Drupal 8,但是我想知道如何在PHPUnit中为Drupal 7编写现有测试(以跟上趋势),而不是在Simpletest中编写?
有没有将PHPUnit与Drupal 7集成的方法或模块?
Answers:
您可以通过在每个测试文件中引导Drupal来运行PHPUnit测试。这不是理想的方法,但是确实有效。
define('DRUPAL_ROOT', 'your/path/to/drupal');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Bootstrap Drupal.
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Proceed with PHPUnit tests as usual from here.
class MyTest extends \PHPUnit_Framework_TestCase {
...
PHPUnit为构建对象提供了一个不错的API,而Drupal的simpletest则没有。gist中有一个库可用于将PHPUnit与Drupal 7集成。
要执行这些脚本,您需要检出该gist-repository。要在命令行中执行单元测试,只需转到Drupal站点(即<DRUPAL_ROOT>/sites/default
)并使用dphpunit.bash,就像使用phpunit命令一样。
该脚本包含3个文件:
资料来源:http : //devblog.more-onion.com/content/writing-unit-tests-drupal-7
bootstrap.inc.php
<?php
$path = CWD;
$site_dir = NULL;
$dpl_dir = NULL;
while ($path != '/') {
if (file_exists($path . '/settings.php')) {
$site_dir = $path;
}
if (file_exists($path . '/index.php') && file_exists($path . '/includes/bootstrap.inc')) {
$dpl_dir = $path;
break;
}
$path = dirname($path);
}
if (!$dpl_dir) {
echo "No drupal directory found in or above current working directory. Aborting. \n";
exit(1);
}
if (!$site_dir) {
$site_dir = $dpl_dir . '/sites/default';
if (!file_exists($site_dir . '/settings.php')) {
echo "No configured site found. Aborting.\n";
exit(1);
}
}
$_SERVER['HTTP_HOST'] = basename($site_dir);
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
define('DRUPAL_ROOT', $dpl_dir);
set_include_path($dpl_dir . PATH_SEPARATOR . get_include_path());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
dphpunit.bash
#!/bin/bash
# get dirname of the script
DIR="$(dirname $(readlink "$0"))"
# assume the boostrap script is stored in the same directory
php "$DIR/drun-phpunit.php" "$(pwd)" --bootstrap "$DIR/bootstrap.inc.php" "$@"
drun-phpunit.php
<?php
require_once 'PHP/CodeCoverage/Filter.php';
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'PHPUNIT');
if (extension_loaded('xdebug')) {
xdebug_disable();
}
if (strpos('/usr/bin/php', '@php_bin') === 0) {
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
}
require_once 'PHPUnit/Autoload.php';
define('PHPUnit_MAIN_METHOD', 'PHPUnit_TextUI_Command::main');
define('CWD', $_SERVER['argv'][1]);
unset($_SERVER['argv'][1]);
$command = new PHPUnit_TextUI_Command;
$command->run($_SERVER['argv']);
还有一个可用于将PHPUnit与Drupal 7集成的库:https : //github.com/sebastianbergmann/phpunit
有关此脚本的更多信息,请参见此处:http : //thomaslattimore.com/blog/using-phpunit-with-drupal-7
<?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="phpunit.xsd" bootstrap="drupal_phpunit_bootstrap.php" verbose="true"> </phpunit>
<?php $_SERVER['HTTP_HOST'] = 'your.url'; $_SERVER['SCRIPT_NAME'] = '/index.php'; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['SERVER_NAME'] = NULL; $_SERVER['SERVER_SOFTWARE'] = NULL; $_SERVER['HTTP_USER_AGENT'] = NULL; // Fix for behat drupal instantiation. define('DRUPAL_ROOT', dirname(realpath(__FILE__))); require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
要点:https : //gist.github.com/permanovd/cb9c02920c49a29c97653f4f697147b1
就这样。现在,您可以通过多种方式开始测试。
phpunit -c phpunit.xml.dist QuestionValidationValueOptionsInputTest /yoursite.dir/public_html/profiles/standard/modules/some_module/tests/QuestionValidationTest.php
哪里:
您必须添加测试运行配置
无需在每个测试中都包含drupal引导程序代码。
由于环境的php版本错误,您可能在测试中遇到问题。