2015-08-14 55 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 
相關問題