单元测试-behat-3

原创 工具
阅读数: 653 2017年06月21日

http://behat.org/en/latest/user_guide/context/definitions.html


------------定义步骤--------------


当Behat运行时,它会比较每个场景中的Gherkin步骤行与FeatureContext中每个方法绑定的模式。

如果Gherkin线满足绑定模式,则执行相应的步骤定义。


Behat 使用 php-doc 注释将模式绑定到 FeatureContext 方法:


<pre class="lang:default decode:true ">&lt;?php

/**

* @When I do something with :methodArgument

*/

public function someMethod($methodArgument) {}

?&gt;</pre>


(注意注释块以/ **开始,而不是通常的/ *。这对于Behat来说是很重要的,以便能够解析注释的注释!)

1、@Given @When @Then 关键词是等效的,都可以随便使用。推荐按语义使用,方便阅读。

2、:后面的关键词是令牌 token,所有的令牌将会被当做对应的变量传入(:methodArgument -&gt; $methodArgument)


<pre class="lang:default decode:true ">#feature

Given I do something with "string1"

When I do something with 'some other string'

Then I do something with 25</pre>


执行结果等于以下效果:


<pre class="lang:default decode:true">$context-&gt;someMethod($methodArgument = 'string1');

$context-&gt;someMethod($methodArgument = 'some other string');

$context-&gt;someMethod($methodArgument = '25');</pre>


(当匹配模式与方法时,Behat不区分步骤关键字。所以与@When定义的一个步骤也可以匹配 @Given ...,@Then ...,@And ...,@But ...等)


绑定模式写法:

1)可以支持传入多个参数:


<pre class="lang:default decode:true ">&lt;?php

/**

* @When I do something with :stringArgument and with :numberArgument

*/

public function someMethod($stringArgument, $numberArgument) {}


?&gt;</pre>


2)指定单词多选方法,例如:is/are,there is xxxx 和 there are xxx 都可以匹配到


<pre class="lang:default decode:true">&lt;?php

/**

* @When there is/are :count monster(s)

*/

public function thereAreMonsters($count) {

echo 'thereAreMonsters-&gt;count:'.$count;

}

?&gt;</pre>


<pre class="lang:default decode:true ">#feature

Feature: You can also specify alternative words and optional parts of words

Scenario: test 1

Given there is 1 monster

Then there are 3 monsters</pre>


输出:


<pre class="lang:default decode:true ">[behat]$ vendor/bin/behat features/default/or.feature 

Feature: You can also specify alternative words and optional parts of words


Scenario: test 1 # features/default/or.feature:2

Given there is 1 monster # FeatureContext::thereAreMonsters()

│ thereAreMonsters-&gt;count:1

Then there are 3 monsters # FeatureContext::thereAreMonsters()

│ thereAreMonsters-&gt;count:3


1 scenario (1 passed)

2 steps (2 passed)

0m0.04s (13.11Mb)</pre>


3)正则匹配方法


<pre class="lang:default decode:true ">&lt;?php

/**

* @When /^there (?:have|had) (\d+) monsters?$/i

*/

public function thereAreMonsters2($count) {

echo 'thereAreMonsters2-&gt;count:'.$count;

}

?&gt;</pre>


<pre class="lang:default decode:true ">#feature

Feature: You can also specify alternative words and optional parts of words

Scenario: test 1

When there have 1 monster

When there had 6 monsters</pre>


输出:


<pre class="lang:default decode:true ">[behat]$ vendor/bin/behat features/default/or.feature 

Feature: You can also specify alternative words and optional parts of words


Scenario: test 1 # features/default/or.feature:2

When there have 1 monster # FeatureContext::thereAreMonsters2()

│ thereAreMonsters2-&gt;count:1

When there had 6 monsters # FeatureContext::thereAreMonsters2()

│ thereAreMonsters2-&gt;count:6


1 scenario (1 passed)

4 steps (4 passed)

0m0.04s (13.12Mb)</pre>


------------定义片段-----definition snippets-------

自动在 Context 类里面,定义片段


先创建一个 feature 文件:

example.feature


<pre class="lang:default decode:true">Feature:

Scenario:

Given some step with "string" argument

And number step with 23</pre>


假设这个 feature 在套件配置里面,指向的是 FeatureContext.php 且在这个文件里面没有设置对应的片段匹配。


执行命令:


<pre class="lang:default decode:true ">[behat]$ vendor/bin/behat features/default/example.feature --dry-run --append-snippets

Feature:


Scenario: # features/default/example.feature:2

Given some step with "string" argument

And number step with 23


1 scenario (1 undefined)

2 steps (2 undefined)

0m0.02s (12.88Mb)


&gt;&gt; default suite has undefined steps. Please choose the context to generate snippets:


[0] None

[1] FeatureContext

&gt; 1


u features/bootstrap/FeatureContext.php - `some step with "string" argument` definition added

u features/bootstrap/FeatureContext.php - `number step with 23` definition added</pre>


会让你选择在哪个 Context 类添加片段,选择1后,将会在 FeatureContext.php 里面添加了简单的片段,如下:


<pre class="lang:default decode:true ">&lt;?php

//...


/**

* @Given some step with :arg1 argument

*/

public function someStepWithArgument($arg1)

{

throw new PendingException();

}


/**

* @Given number step with :arg1

*/

public function numberStepWith($arg1)

{

throw new PendingException();

}

?&gt;</pre>


这就完成了自动创建片段。


phpriji.cn | 网站地图 | 沪ICP备17015433号-1