在Behat的特征文件中使用占位符

时间:2017-03-03 07:16:16

标签: testing automated-tests symfony bdd behat

我有一个功能文件,如下所示

    Feature: Test send API request
    In order to test my API
    As a Tester
    I want to be able to perform HTTP request


    Scenario:Sending GET request to activate user after registration api to verify whether the response code is 403 when 'X-Auth-Token' is missing
            When I have a request "GET /api/activateuser?token=:tokenhash"
            And I set the "Accept" header to "application/json"
            And I set the "X-Auth-Token" header to "0125ee8dfe42bafbec95aa0d2676c91d8a780715b76504cf798aae6e74c08a30"
            .
            .

    Scenario:Sending GET request to activate user after registration api to verify whether the response code is 403 when 'X-Auth-Token' is invalid
            When I have a request "GET /api/activateuser?token=:tokenhash"
            And I set the "Accept" header to "application/json"
            And I set the "X-Auth-Token" header to "0125ee8dfe42bafbec95aa0d2676c91d8a780715b76504cf798aae6e74c08a30"
            .
            .

    Scenario:Sending GET request to activate user after registration api to verify whether the response code is 404 when userid is invalid
            When I have a request "GET /api/activateuser?token=:tokenhash"
            And I set the "Accept" header to "application/json"
            And I set the "X-Auth-Token" header to "0125ee8dfe42bafbec95aa0d2676c91d8a780715b76504cf798aae6e74c08a30"
            .
            .

在请求中,'X-Auth-Token'参数对于所有scnerios都是相同的,不会经常更改。所以我想将它设置为某个变量并在场景中使用该变量。但是没有找到任何方法来做到这一点。即使我们可以在behat.yml中设置值并在场景中使用它也没关系,但即使这样也是不可能的。

此外,我还需要设置多个参数。

那么有没有方法可以设置一次值并在每个场景中重新使用它?

1 个答案:

答案 0 :(得分:1)

您可以使用两种组合。

  1. 一个Background,您可以在其中运行所有方案的所有常用步骤。
  2. 准备当前测试范围的BeforeFeature挂钩。
  3. 下面会发生什么。

    1. @BeforeScenario代码在其他所有内容之前运行prepare()方法,为当前要素会话设置变量。

    2. Background任务下的步骤定义在每个方案之前运行,因此您不必在每个方案中复制它们。

    3. 注意:如果您的X-Auth-Token不会经常更改,那么只需对您的FeatureContext文件中的值进行硬编码,然后根本不执行上述第2步。我的例子是让你了解Behat的一些有用功能。

      示例

      根据您的需要进行调整!

      FeatureContext

      namespace Your\Bundle\Features\Context;
      
      use Behat\Behat\Hook\Scope\BeforeScenarioScope;
      ...
      
      class FeatureContext ...
      {
          private static $xAuthToken;
      
          /**
           * @BeforeFeature
           */
          public static function prepare()
          {
              self::setXAuthToken();
          }
      
          private static function setXAuthToken()
          {
              self::$xAuthToken = 123;
          }
      
          /**
           * @Given /^I set the header "([^"]*)" to "([^"]*)"$/
           */
          public function iSetTheHeader($header, $value)
          {
              // Do whatever you want
          }
      
          /**
           * @Given /^I send "([^"]*)" request to "([^"]*)"$/
           */
          public function iSendRequest($method, $url)
          {
              // Do whatever you want
          }
      
          /**
           * @Given /^the X-Auth-Token is available$/
           */
          public function theXAuthTokenIsAvailable()
          {
              echo self::$xAuthToken;
          }
      }
      

      功能文件

      Feature: Shared token
      
        Background: I am common to all scenarios
          Given I set the header "Accept" to "application/json"
          When I send "GET" request to "/api/hello-world"
      
        Scenario: 1
          Given the X-Auth-Token is available
      
        Scenario: 2
          Given the X-Auth-Token is available
      

      <强> RESULT

      enter image description here