2015-08-14 49 views
0

我有一个步骤定义,我希望通过功能文件输入多个pystrings。这是可能的,如果是的话,你会用什么语法来做到这一点?使用Behat时可以在Step Definition中使用多个PyStrings吗?

目前我的步骤定义是这样的:

@Then /^(?:I)?get a value "([^"]+)" when I have a username "([^"]+)" and a password "([^"]+)" and I send a "([A-Z]+)" request to "([^"]+)" with form data: and header data:$/ 

然后我的实际特征文件的样子:

Then I get a value "orange" when I have a username "jsmith" and a password "password" and I send a "POST" request to "www.google.com" with form data: 

    """ 
      { 
       "key1": "value1", 
       "key2": "value2", 
      } 
    """ 
and header data: 

    """ 
      { 
       "key1": "value1", 
       "key2": "value2", 
      } 
    """ 
+0

你的整个'Then'定义好像你正在处理在一个单一的一个,这是非常丑陋的我多种定义。这就像'当我打开门,然后关上门然后我再次打开门然后我一步就看不到任何东西,而不是四行。如果我是你,我会重构整个事情。这是丑陋的,不可读的,不友好的用户界面,注视你的名字! – BentCoder

+0

我知道它很丑,但我也遇到了将一个步骤定义中的所有标题和发布值保留到下一个 - 有必要的问题。对于每个定制的步骤,似乎都将重置共享公共变量。也许这会是一个更好的问题。 – Nabsta

回答

0

对于这似乎是重置共享公共变量的每一步定制。

它不应该发生!

您可以分隔所有这些行,如下所示,将数据存储在变量中,并在最后处理它们,因此您不必一次完成所有操作。如果其中一个必须在中间运行,那么你可以。

一些例子:

http://www.inanzzz.com/index.php/post/ajqn/api-request-response-testing-with-behat-v2-includes-json-xml-html-and-cli

http://www.inanzzz.com/index.php/post/xw1v/api-request-response-testing-with-behat-v1

FeatureContext

class FeatureContext extends MinkContext 
{ 
    private $value; 
    private $username; 
    private $password; 

    /** 
    * @When /^I get a value "([^"]*)"$/ 
    */ 
    public function iGetValue($value) 
    { 
     $this->value = $value; 
    } 

    /** 
    * @When /^I have the username "([^"]*)"$/ 
    */ 
    public function iHaveUsername($username) 
    { 
     $this->username = $username; 
    } 

    /** 
    * @When /^I have the password "([^"]*)"$/ 
    */ 
    public function iHavePassword($password) 
    { 
     $this->password = $password; 
    } 

    /** 
    * @When /^I send a "([^"]*)" request to "([^"]*)" with form data:$/ 
    */ 
    public function iSendRequestToWithFormData($method, $address, PyStringNode $requestPayload) 
    { 
     // Example is here: http://www.inanzzz.com/index.php/post/ajqn/api-request-response-testing-with-behat-v2-includes-json-xml-html-and-cli 
     // @When /^I send a "([^"]*)" request to "([^"]*)"$/ 
    } 

    /** 
    * @When /^I have header data:$/ 
    */ 
    public function iHaveHeaderData(PyStringNode $requestPayload) 
    { 
     // Example is here: http://www.inanzzz.com/index.php/post/ajqn/api-request-response-testing-with-behat-v2-includes-json-xml-html-and-cli 
    } 

    /** 
    * @When /^You run this step to do whatever you wanted to do with your steps above$/ 
    */ 
    public function iRun() 
    { 
     echo $this->value . PHP_EOL; 
     echo $this->username . PHP_EOL; 
     echo $this->password . PHP_EOL; 
    } 
} 

小黄瓜

Scenario: I test things 
    When I get a value "69" 
    Then I have the username "hello" 
    And I have the password "world" 
    And I send a "POST" request to "www.google.com" with form data: 
    """ 
    { 
     "key1": "value1", 
     "key2": "value2" 
    } 
    """ 
    And I have header data: 
    """ 
    { 
     "key1": "value1", 
     "key2": "value2" 
    } 
    """ 
    Then You run this step to do whatever you wanted to do with your steps above 

结果

Feature: Whatever 

    Scenario: I test things 
    When I get a value "69" 
    Then I have the username "hello" 
    And I have the password "world" 
    And I send a "POST" request to "www.google.com" with form data: 
     """ 
     { 
     "key1": "value1", 
     "key2": "value2" 
     } 
     """ 
    And I have header data: 
     """ 
     { 
     "key1": "value1", 
     "key2": "value2" 
     } 
     """ 
69 
hello 
world 
    Then You run this step to do whatever you wanted to do with your steps above 

1 scenario (1 passed) 
6 steps (6 passed) 
0m0.857s 
相关问题