【发布时间】:2015-06-11 23:01:12
【问题描述】:
问题
我正在使用 SpecFlow 为 REST 服务创建集成测试套件。
我正在以多种不同的配置运行该套件。 (我有多个构建配置,每个都有自己的一组 app.config 转换。)
在 C# 代码中,检查配置并根据它执行不同的代码非常简单。我可以简单地做这样的事情。
[Given(@"I set the test parameter to ""(.*)""")]
public void GivenISetTheTestParameter(string parameter)
{
if(CurrentConfiguration == Config.Test)
this.testParameter = parameter + "Test";
else if(CurrentConfiguration == Config.Prod)
this.testParameter = parameter + "Prod";
}
这种方法的问题在于它在每次执行此步骤时都以相同的方式工作,但我不想在每种情况下都以不同的方式参数化该步骤的配置相关部分。
feature 文件中有什么方法可以做到这一点吗?我想做这样的事情(伪代码,这当然行不通):
If (CurrentConfiguration == Config.Test)
Given I set the test parameter to "ParameterTest"
Else If (CurrentConfiguration == Config.Prod)
Given I set the test parameter to "ParameterProd"
然后我可以在每个场景中以不同的方式使用这个参数化:
Scenario: Test 1
If (CurrentConfiguration == Config.Test)
Given I set the test parameter to "ParameterTest1"
Else If (CurrentConfiguration == Config.Prod)
Given I set the test parameter to "ParameterProd1"
...
Scenario: Test 2
If (CurrentConfiguration == Config.Test)
Given I set the test parameter to "ParameterTest2"
Else If (CurrentConfiguration == Config.Prod)
Given I set the test parameter to "ParameterProd2"
...
如果条件是在该步骤的 C# 代码中实现的,则这是不可能的。
现实世界的例子
我想用它来集成测试 REST 服务。假设我使用基本身份验证,为此我需要在 RestClient 对象上设置一个标头。
我有一个帮助步骤,用于将 auth 标头设置为特定的用户名和密码。
棘手的部分是我有多个构建配置(比如说 Staging 和 Prod),为此我需要不同的测试凭据。另外,我在功能的不同场景中调用不同的 API,这也需要不同的凭据。
所以有了上面介绍的伪语法,这就是我想做的:
Scenario: Test LoggingService
If (CurrentConfiguration == Config.Test)
Given I set the auth header for the user "logging_test_user" and password "p4ssword"
Else If (CurrentConfiguration == Config.Prod)
Given I set the auth header for the user "logging_prod_user" and password "p4ssword"
...
When I call the LoggingService
...
Scenario: Test PaymentService
If (CurrentConfiguration == Config.Test)
Given I set the auth header for the user "payment_test_user" and password "p4ssword"
Else If (CurrentConfiguration == Config.Prod)
Given I set the auth header for the user "payment_prod_user" and password "p4ssword"
...
When I call the PaymentService
...
如果我只能将条件放入“鉴于我设置了身份验证标头...”步骤的 C# 实现中,那么我将无法为不同的场景指定不同的用户名。
【问题讨论】:
-
您能否提供更多背景信息以及一个类似真实世界的示例?我很难理解为什么你需要把它放在功能文件中而不是直接从
System.Configuration.ConfigurationManager读取它? -
添加了另一个示例,说明拥有条件特征文件的优势。
-
我正在寻找一个真实的用例场景来说明您为什么需要它。您说您正在测试 REST Web 服务。 App.config 中的哪些数据发生变化,需要您以不同的方式测试服务?
-
我想我想说的是,“为什么”你想这样做?我有一种感觉,您的问题的解决方案与您想象的大不相同。
-
添加了一个真实世界的例子。