【发布时间】:2022-02-23 18:11:31
【问题描述】:
我对 Specflow 还很陌生,我正在尝试使用 Specflow BeforeFeature Hook 看看这是否可行。我有一些仅用于特定功能的动态测试数据设置。我想将这些测试数据值用作场景测试的一部分。这是功能文件的场景测试之一:
@logintest
Feature: User login
Scenario: User login with different product access
Given I am in <productType> login page
When I login as <user>
I should see <productType> logo in my account
在 BeforeFeature() 中,我们有生成的函数: -具有产品访问类型属性的随机用户帐户 - 产品登录页面的随机网址。 (例如:productA234234.dev.local)
[Binding]
public class LoginHooks
{
[BeforeFeature("logintest")]
public static void BeforeFeature()
{
UserAccount testUser = new UserAccount(productType1);
var product1Url = CreateProductUrl(productType1);
FeatureContext.Current.Set("testUser", testUser);
FeatureContext.Current.Set("productUrl", productUrl);
...
}
因为每次运行测试时都会随机生成用户名,所以我无法将用户名作为数据表等的一部分提供给 Gherkins。有没有办法构建 Gherkins 以从 FeatureContext 中获取值?
[Binding]
public class UserLoginSteps
{
//var productUrl = ???
[Given(@"I am in (.*) login page")]
public void GivenIAmInLoginPage(string productUrl)
{ . . .}
}
问题:
- 是否可以获取 productUrl 的 FeatureContext 值并在步骤中设置?
- 我认为在这种情况下给定的正则表达式是不正确的。是否有另一种方法来构造它以将变量作为数据输入的一部分?
谢谢!
【问题讨论】: