Behat Drupal:访问%paths.base%之外的功能

时间:2017-02-01 16:40:10

标签: drupal drupal-7 automated-tests behat

我的任务是在我们现有的基于Drupal系统的分发中改进behat测试。问题是我想运行来自不同目录的behat测试相互引用。我们支持多个D7站点并以我们的内部分发为基础,因此所有站点都可以共享创建相同类型功能的模块。分发模块的示例是:dist_news和dist_events,以及基于分布的测试将存在于dist_test模块中。这些模块将在诸如dentist.oursite.org或radio.oursite.org之类的不同站点上创建新闻和事件内容类型,并且每个模块在git中都有自己的repo。特定的站点位于一个名为dentist_builder或radio_builder的仓库中,这两个仓库都基于dist_builder,它位于自己的仓库中,并通过组合器,grunt,纱线等从单个模块回购建立。特定于站点的测试存在于模块中叫做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

但测试In In News部分新闻的情景会失败,因为它不在Radio上。

所以我想设置的是让测试通过dentist_test的behat.yml或radio_test的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 / tests / contexts / 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访问dist_tests中的news.feature,我可以用路径和标签修复其余部分,我认为,我现在只需要了解如何获得这些功能。我想要避免的是在dentist_test和radio_test中复制和维护相同的基于分布的测试,因为一旦我们进入数百个将变得荒谬的网站。 我假设

paths:
      - %paths.base%/../../../../../../profiles/dist/modules/dist/dist_test/tests/features/news

是我出错的地方,或者甚至可以做我想做的事情?第一次潜入behat,如果我错过了一些简单的架构,请告诉我。

1 个答案:

答案 0 :(得分:1)

原来它是路径,只是让我输入所有这些来解决它。 改变:

paths:
  - %paths.base%/../../../../../../profiles/dist/modules/dist/dist_test/tests/features/news

为:

paths:
  - %paths.base%/../../../../profiles/dist/modules/dist/dist_test/tests/features/news

我错过的是,dental_test生活在src / modules / dentist_test,但在网站构建期间符号链接到build / html / sites / all / modules / custom / dental_test。我需要从前者而不是后者进入。现在它很棒!但如果有人有更好的方法,请告诉我。