SpecFlow - 一个特征两个数据集
SpecFlow - one featuretwo data sets
我有两个数据集,我想根据测试 运行 在哪台机器上使用它们。如何在 specflow 中执行此操作?
Scenario: The user logs in using the confirmed account
When Logging in to the account: user1/user2
Then Login succesful
如果在机器 1 上测试 运行,那么我使用 user1 帐户,如果他们在机器 2 上 运行,我使用 user2 帐户。怎么做?
有几种方法可以实现这一点。最简单的方法可能是将用户名存储在 Dictionary
中,其中键是主机名,值是用户名。
这涉及3个部分:
创建配置class
public class TestConfiguration
{
private readonly Dictionary<string, string> passwords;
private readonly Dictionary<string, string> usernames;
/// <summary>
/// Returns the test password based on the current machine
/// </summary>
public string Password => passwords[Environment.MachineName];
/// <summary>
/// Returns the test username based on the current machine
/// </summary>
public string Username => usernames[Environment.MachineName];
public TestConfiguration()
{
passwords = new Dictionary<string, string>()
{
{ "hostname1", "password1" },
{ "hostname2", "password2" }
};
usernames = new Dictionary<string, string>()
{
{ "hostname1", "username1" },
{ "hostname2", "username2" }
};
}
}
将其注册到 SpecFlow 依赖注入框架
[Binding]
public class SpecFlowHooks
{
private readonly IObjectContainer container;
public SpecFlowHooks(IObjectContainer container)
{
this.container = container;
}
[BeforeScenario]
public void BeforeScenario()
{
var config = new TestConfiguration();
container.RegisterInstanceAs(config);
}
}
在步骤定义中使用 TestConfiguration
[Binding]
public class LoginSteps
{
private readonly TestConfiguration config;
public LoginSteps(TestConfiguration config)
{
this.config = config;
}
[Given(@"the user is logged in")]
public void GivenTheUserIsLoggedIn()
{
// Log in using config.Username and config.Password
}
}
如果您需要任何其他步骤定义中的用户信息 classes,只需将 TestConfiguration config
参数添加到该 class 的构造函数中即可。有关如何通过 SpecFlow 使用依赖项注入的更多信息,请参阅 Context Injection。
我有两个数据集,我想根据测试 运行 在哪台机器上使用它们。如何在 specflow 中执行此操作?
Scenario: The user logs in using the confirmed account When Logging in to the account: user1/user2 Then Login succesful
如果在机器 1 上测试 运行,那么我使用 user1 帐户,如果他们在机器 2 上 运行,我使用 user2 帐户。怎么做?
有几种方法可以实现这一点。最简单的方法可能是将用户名存储在 Dictionary
中,其中键是主机名,值是用户名。
这涉及3个部分:
创建配置class
public class TestConfiguration { private readonly Dictionary<string, string> passwords; private readonly Dictionary<string, string> usernames; /// <summary> /// Returns the test password based on the current machine /// </summary> public string Password => passwords[Environment.MachineName]; /// <summary> /// Returns the test username based on the current machine /// </summary> public string Username => usernames[Environment.MachineName]; public TestConfiguration() { passwords = new Dictionary<string, string>() { { "hostname1", "password1" }, { "hostname2", "password2" } }; usernames = new Dictionary<string, string>() { { "hostname1", "username1" }, { "hostname2", "username2" } }; } }
将其注册到 SpecFlow 依赖注入框架
[Binding] public class SpecFlowHooks { private readonly IObjectContainer container; public SpecFlowHooks(IObjectContainer container) { this.container = container; } [BeforeScenario] public void BeforeScenario() { var config = new TestConfiguration(); container.RegisterInstanceAs(config); } }
在步骤定义中使用 TestConfiguration
[Binding] public class LoginSteps { private readonly TestConfiguration config; public LoginSteps(TestConfiguration config) { this.config = config; } [Given(@"the user is logged in")] public void GivenTheUserIsLoggedIn() { // Log in using config.Username and config.Password } }
如果您需要任何其他步骤定义中的用户信息 classes,只需将 TestConfiguration config
参数添加到该 class 的构造函数中即可。有关如何通过 SpecFlow 使用依赖项注入的更多信息,请参阅 Context Injection。