【问题标题】:Behat Drupal: accessing features outside the %paths.base%Behat Drupal:访问 %paths.base% 之外的功能
【发布时间】:2017-02-01 16:40:09
【问题描述】:

我的任务是改进我们现有的基于发行版的 Drupal 系统中的行为测试。问题是我想从相互引用的不同目录运行 behat 测试。我们支持多个 D7 站点并将它们基于我们的内部分发,因此所有站点都可以共享创建相同类型功能的模块。分发模块的示例是:dist_news 和 dist_events,并且基于分发的测试将存在于 dist_test 模块中。这些模块将在不同的站点(如 Dental.oursite.org 或 radio.oursite.org)中创建新闻和事件内容类型,并且每个模块在 git 中都有自己的存储库。特定站点位于名为dentity_builder 或radio_builder 的存储库中,它们都基于位于自己的存储库中的dist_builder,并通过composer、grunt、yarn 等从单个模块存储库构建自身。站点特定测试位于模块中名为dental_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 The News 部分的场景会失败,因为它不在 Radio 中。

所以我要设置的是让测试通过 Dental_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

dental_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)
}

一旦我弄清楚如何从dist_tests 中的behat.yml 中访问news.feature,我可以用我认为的路径和标签来修复其余部分,我现在只需要知道如何使用这些功能。我要避免的是在dental_test 和radio_test 中复制和维护相同的基于分布的测试,因为一旦我们进入数百个站点,这将变得荒谬。 我假设

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

是我哪里出错了,还是可以做我想做的事?第一次深入了解行为,所以如果我遗漏了一些简单的架构方面的知识,请告诉我。

【问题讨论】:

    标签: drupal drupal-7 automated-tests behat


    【解决方案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。我需要从前者而不是后者开始。现在效果很好!但如果有人有更好的方法,请告诉我。

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2020-06-10
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多