我的任务是在我们现有的基于Drupal系统的发行版中改进behat测试。问题是我想从相互引用的不同目录中运行behat测试。我们支持多个D7站点,并以我们的内部分发为基础,因此所有站点都可以共享创建相同功能的模块。分发模块的示例是: dist_news和dist_events,而对于behat,基于分发的测试将位于dist_test模块中。这些模块将在dentist.oursite.org或radio.oursite.org等独立网站上创建新闻和事件内容类型,并且每个模块在git中都有自己的repo。特定的站点驻留在名为dentist_builder或radio_builder的存储库中,这两个存储库都基于dist_builder,它位于自己的存储库中,并通过composer、grunt、yarn等从单个模块存储库构建自己。站点特定的测试位于名为dentist_test的模块中,该模块位于牙医构建器中。当我想要测试一个站点上的功能而不是另一个站点上的功能时,问题就出现了。例如。牙医网站有新闻和事件,广播网站有新闻但没有事件,关于牙医的新闻在新闻部分有,而广播网站没有。新闻和事件的各种测试都在dist_test中,所以如果我想测试它们,可以运行如下命令:
vendor/bin/behat -c build/html/profiles/dist/modules/dist/dist_test/tests/behat.yml build/html/profiles/dist/modules/dist/dist_test/tests/features/news/news.feature
vendor/bin/behat -c build/html/profiles/dist/modules/dist/dist_test/tests/behat.yml build/html/profiles/dist/modules/dist/dist_test/tests/features/events/events.feature
而对于无线电,我可以运行:
vendor/bin/behat -c build/html/profiles/dist/modules/dist/dist_test/tests/behat.yml build/html/profiles/dist/modules/dist/dist_test/tests/features/news/news.feature
但是测试新闻中的新闻部分的场景将会失败,因为它不在广播中。
所以我正在尝试设置的是让测试通过牙医测试的behat.yml或无线电测试的behat.yml运行,这样我就只能测试我想在每个站点上测试的东西:
vendor/bin/behat -c build/html/src/modules/dentist_test/tests/behat.yml --suite=news
vendor/bin/behat -c build/html/src/modules/radio_test/tests/behat.yml --suite=news
在那里,相应的套件将运行所有测试,通过标签过滤器或路径,为牙医网站和只有我们需要的测试电台网站。
Dentist_builder,包括到目前为止工作的测试,具有相关的目录结构:
vendor/
bin/
behat
build/
html/
profiles/
dist/
modules/
dist/
dist_test/
tests/
behat.yml
contexts/
/FeatureContext.php
features/
/news
/news.feature
src/
modules/
dentist_test/
tests/
behat.yml
features/
/homepage.feature
dentist_test中的behat.yml设置如下:
default:
suites:
default:
contexts:
- Dist\Context\FeatureContext:
- screenshotDirectory: '%paths.base%/screenshots'
- Drupal\DrupalExtension\Context\DrupalContext
- Drupal\DrupalExtension\Context\MinkContext
- Drupal\DrupalExtension\Context\MessageContext
- Drupal\DrupalExtension\Context\DrushContext
我尝试设置另一个套件来使用dist_test中的特性,但它不起作用:
news:
contexts:
- Dist\Context\FeatureContext:
- screenshotDirectory: '%paths.base%/screenshots'
- Drupal\DrupalExtension\Context\DrupalContext
- Drupal\DrupalExtension\Context\MinkContext
- Drupal\DrupalExtension\Context\MessageContext
- Drupal\DrupalExtension\Context\DrushContext
filters:
tags: "@news"
paths:
- %paths.base%/../../../../../../profiles/dist/modules/dist/dist_test/tests/features/news
dist_test/test/context/FeatureContext.php如下所示:
<?php
/**
* @file
* Default context class defining how behat should test our application.
*
* @see http://docs.behat.org/en/latest/guides/4.contexts.html
*/
namespace Dist\Context;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\Exception\ElementNotFoundException;
use Symfony\Component\DependencyInjection\Container;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends RawDrupalContext implements SnippetAcceptingContext {
/**
* Initializes context.
*
* Every scenario gets its own context instance.
*/
public function __construct($parameters = array()) {
foreach ($parameters as $key => $value) {
$property = lcfirst(Container::camelize($key));
$this->$property = $value;
}
}
/**
* Custom step to assert that username of user with uid is not listed.
*
* @Then I should not see user :uid
*/
public function iShouldNotSeeUser($uid) {
$user = \user_load($uid);
$this->assertSession()->pageTextNotContains($user->name);
}
... (a bunch more functions)
}
一旦我弄清楚如何从dentist_test中的behat.yml访问dentist_test中的news.feature,我就可以用路径和标签修复剩下的部分,现在我只需要知道如何使用这些功能。我想要避免的是在dentist_test和radio_test中复制和维护相同的基于发行版的测试,因为一旦我们进入成百上千的站点,就会变得荒谬。我假设
paths:
- %paths.base%/../../../../../../profiles/dist/modules/dist/dist_test/tests/features/news
是我错了,还是behat能做我想让它做的事?第一次深入到behat,所以如果我遗漏了一些简单的架构智慧,请让我知道。
发布于 2017-02-02 05:32:28
原来就是这条路,我敲了这么多才弄明白。已更改:
paths:
- %paths.base%/../../../../../../profiles/dist/modules/dist/dist_test/tests/features/news
至:
paths:
- %paths.base%/../../../../profiles/dist/modules/dist/dist_test/tests/features/news
我遗漏的一件事是,测试位于src/modules/ dental_test _test,但在站点的构建过程中是符号链接到build/html/sites/all/modules/custom/dental_test的。我需要从前者开始,而不是后者。现在它工作得很好!但如果有人有更好的方法,请让我知道。
https://stackoverflow.com/questions/41985015
复制相似问题