>问题
我正在使用 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";
}
这种方法的问题在于,它对于此步骤的每次执行都以相同的方式工作,但我不想在每种情况下都以不同的方式参数化步骤的配置相关部分。
有什么方法可以在功能文件中执行此操作吗?我想做这样的事情(伪代码,这当然不起作用):
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
对象上设置标头。
我有一个帮助程序步骤,用于将身份验证标头设置为特定的用户名和密码。
棘手的部分是我有多个构建配置(假设暂存和生产),为此我需要不同的测试凭据。此外,我在功能的不同方案中调用不同的 API,这些 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# 实现中步骤,那么我将无法为不同的场景指定不同的用户名。
您根本不希望功能文件中的可配置数据。相反,创建一个通用步骤,其定义读取配置文件:
Scenario: Test LoggingService
Given I set the auth header
在 C# 中:
[Given(@"I set the auth header")]
public void GivenISetTheAuthHeader()
{
string username = System.Configuration.ConfigurationManager.AppSettings["RestServiceUserName"];
string password = System.Configuration.ConfigurationManager.AppSettings["RestServicePassword"];
}
在应用程序配置中:
<appSettings>
<add key="RestServiceUserName" value="..."/>
<add key="RestServicePassword" value="..."/>
如果不同的用户名在系统中具有不同的权限,请考虑改用方案大纲:
Scenario Outline: Testing the LoggingService
Given I set the auth header for user "<Username>" and password "<Password>"
Examples:
| Username | Password |
| user1 | pass1 |
| user2 | pass2 |
它们成为步骤定义的正常参数:
[Given("I set the auth header for user """(.*)""" and password """(.*)"""")]
public void GivenISetTheAuthHeaderForUserAndPassword(string username, string password)
{
// set the user and password on the auth header
}
场景大纲和标记示例来实现您想要的,但是您只需要在某些环境中运行一些测试:
Scenario Outline: Testing the LoggingService
Given I set the auth header for user "<Username>" and password "<Password>"
@Production
Examples:
| Username | Password |
| logging_prod_user | p4ssword |
@Test
Examples:
| Username | Password |
| logging_test_user | p4assword |
然后将测试运行程序配置为仅运行某些类别(Test
或Production
) 中的测试
如果使用 nunit(或 XUnit 或任何其他默认使用行测试来运行方案大纲的测试运行程序)作为测试运行程序,请注意此问题
我们为不同的环境做了类似的事情,但我们有一个用于测试的 app.config,它有几个用于开发、qa 和 uat 的"替代"配置,我们从其中一个部分读取命名参数的值。
我们有这样的东西
<testingEnvironments>
<testingEnvironment name="Dev" messageUrl="https://somedevurl/" isCurrent="true">
<ConfigParam1>SomeValue</ConfigParam1>
</testingEnvironment>
<testingEnvironment name="QA" messageUrl="https://somedqaurl/" isCurrent="false">
<ConfigParam1>SomeValueInQA</ConfigParam1>
</testingEnvironment>
<testingEnvironment name="UAT" messageUrl="https://someuaturl/" isCurrent="false">
<ConfigParam1>SomeValueUAT</ConfigParam1>
</testingEnvironment>
</testingEnvironments>
我们根据 isCurrent
属性的值选择配置,但您可以根据环境变量的名称选择它。
那么你的测试不知道使用的确切值,那么只参考ConfigParam1
根据您的真实示例,我不喜欢测试中的实现细节(如果您使用其他身份验证机制怎么办),并且会像这样重组我的规范:
Scenario: Test LoggingService
Given I am the logging service default user for the current environment
When I call the LoggingService
...
Scenario: Test payment Service
Given I am the payment service default user for the current environment
When I call the PaymentService
...
并会添加这样的配置:
<testingEnvironment name="DEV" messageUrl="https://somedevurl/" isCurrent="false">
<userCredentials>
<LoggingService>
<defaultUser name="logging_test_user" password="p4ssword" />
</LoggingService>
<PaymentService>
<defaultUser name="payment_test_user" password="p4ssword" />
</PaymentService>
</userCredentials>
</testingEnvironment>
<testingEnvironment name="UAT" messageUrl="https://someuaturl/" isCurrent="false">
<userCredentials>
<LoggingService>
<defaultUser name="logging_prod_user" password="p4ssword" />
</LoggingService>
<PaymentService>
<defaultUser name="payment_prod_user" password="p4ssword" />
</PaymentService>
</userCredentials>
</testingEnvironment>
然后,您的各个步骤可以调用一个通用步骤来设置实际的标头值
你的测试应该始终是相同的——一个带有"if"的测试至少是两个测试。解决此问题的正确方法是隔离受测系统,使其采用表示配置值的参数(或以其他方式提供值),然后为所有适用方案编写测试。
我会写这个功能:
Scenario: Test LoggingService
Given I set the auth header with valid user and password
When I call the LoggingService
# ...
设置App.config
文件:
<appSettings>
<add key="EnvironmentUserName" value="..."/>
<add key="EnvironmentPassword" value="..."/>
<!-- ... -->
</appSettings>
并将步骤实现为:
public void GivenISetTheAuthHeader()
{
string username = System.Configuration.ConfigurationManager.AppSettings["EnvironmentUserName"];
string password = System.Configuration.ConfigurationManager.AppSettings["EnvironmentPassword"];
// ...
}