2017-03-03 50 views
0

的特征文件中使用的占位符我有一个​​功能文件,如下在贝哈特

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-验证令牌”参数都是一样的所有scnerios,不会频繁地改变。所以我正在考虑将它设置为某个变量并在场景中使用该变量。但没有找到任何方法来做到这一点behat。即使我们可以在behat.yml中设置值并在场景中使用它也没关系,但即使这样也是不可能的。

另外我有一个以上的参数需要像这样设置。

那么是否有任何方法来设置值一次,并在每种情况下重用它?

回答

1

您可以使用两种组合。

  1. A Background其中您运行所有场景的所有常用步骤。
  2. A BeforeFeature挂钩,准备您当前的测试范围。

下面会发生什么是这个。

  1. @BeforeScenario标签运行一切来设置可变电流特性会议之前prepare()方法。

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

注:如果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 

结果

enter image description here