为基于资源的api实现specflow bdd的混乱



我第一次尝试将bdd实现到项目中,并对受众使用的语言有几个问题。

我看过很多例子,其中的语言是为了让最终用户等理解。但是,如果你正在编写一个api(支持odata的rest风格的web api),而它只会被其他开发人员使用呢?可以更具体一点吗?还是应该尽量简单一点?

其次,如果你在一般意义上描述它,你是否必须在中指定值,例如:

Scenario: product is defined for the first time
Given product code abcdefg does not already exist
When product definition with code abcdefg is uploaded
Then product is created
And user receives status 201 created with url 'blah'

但是,把它抽象成一个更一般的情况可以吗?例如

Scenario: product is defined for the first time
Given product code does not already exist
When product definition is uploaded
then product is created
And user receives status 201 and creation url

感谢

更新

多亏了sam和AlSki,我们决定将我们的规格分为3种不同的类型:

User: End-User facing, most declarative style concerning UI
Dev: More imperative and detailed, concerning data only interactions
System: Most imperative and detailed - concerning our internal stuff

我们现在用多个例子创建了Scenario Outlines,这些例子似乎可以很好地测试我们所有的边缘案例。

我还删除了外部开发人员无法观察到的结果("产品已经创建"等),我认为我们现在都更好地理解了这一点。

第一个问题的答案是肯定的。你可以在BDD中做任何你想做的事,只要它对使用它的人有意义。

因此,如果您是一个完全技术化的团队,并且都乐于使用状态代码201url,那么继续吧。

读一读BDD倡导者(Dan North、Liz Keogh、Gojko Adzic等),你会发现重要的一点是与其他人交谈,并在你的规范中使用与你在谈话中使用的相同的语言,因为你正在封装你的知识,并用它来测试

然而,你的第二个例子可能太远了。考虑BDD也被称为规范,示例,在这种情况下,您已经删除了该示例。你只是有一个大致的想法,但也没有什么具体的测试。

考虑一下,如果出于某种原因(我们都见过),有一个特殊的产品代码可以做一些不同的事情。你将如何测试?

你可以做的是提取出通用的步骤,但在测试中保持特定性:

Scenario: product is defined for the first time
   Given product code 'abcdefg' does not already exist
   When product definition with code 'abcdefg' is uploaded
   Then product 'abcdefg' is created
   And user receives status 201 created with url 'blah'

它与以下步骤结合:

[Given("product code '(.*)' does not already exist")]
public void ProductCodeDoesNotExist(string productCode)
{...}

然后你可以重复使用相同的步骤来测试许多产品

此外,我建议您更改步骤:

 And user receives status 201 created with url 'blah'

到一些不太具体的实现,比如:

 And the product is created successfully at the location 'blah'

因此,它们的技术性较低,您可以重用这些步骤来驱动在API层和UI层进行测试的测试,只需简单地替换用于绑定步骤的步骤实现即可。

事实上,我可能会合并你的两个Then步骤,或者有这个:

Then product 'abcdefg' is created successfully
And the created product is available at the location 'blah'

因为我不确定你在中可以做什么检查

then product is created

中没有暗示

And user receives status 201 and creation url

最新更新