【问题标题】:Using placeholders in feature file of Behat在 Behat 的功能文件中使用占位符
【发布时间】:2017-03-03 07:16:15
【问题描述】:

我有一个功能文件如下

    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 中设置值并在场景中使用它也没关系,但即使这样也不可能。

我还有不止一个参数需要这样设置。

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

【问题讨论】:

    标签: testing automated-tests symfony bdd behat


    【解决方案1】:

    您可以使用两者的组合。

    1. Background,您可以在其中针对所有场景运行所有常用步骤。
    2. 一个 BeforeFeature 钩子,用于准备您当前的测试范围。

    下面发生的事情是这样的。

    1. @BeforeScenario 标记运行prepare() 方法,然后为当前功能会话设置变量。

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

    注意:如果您的 X-Auth-Token 不会经常更改,那么只需在 FeatureContext 文件中硬编码该值,并且根本不执行上述第 2 步。我的示例旨在让您了解 Behat 的一些有用功能。

    示例

    根据您的需要调整它!

    特征上下文

    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
    

    结果

    【讨论】:

      猜你喜欢
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 2012-09-04
      • 2012-01-04
      • 2012-03-12
      • 2012-07-26
      相关资源
      最近更新 更多