gpt4 book ai didi

c# - 是否可以在 beforeFeature Hook 中使用变量作为 Specflow 场景语句的一部分?

转载 作者:行者123 更新时间:2023-11-30 16:44:19 25 4
gpt4 key购买 nike

我是 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)
{ . . .}
}

问题:

  1. 是否可以获取 productUrl 的 FeatureContext 值并在步骤中设置?
  2. 我认为在这种情况下 Given 的正则表达式是不正确的。是否有另一种方法来构造它以将变量作为数据输入的一部分?

谢谢!

最佳答案

使用 BDD 时使用随机数据是一种糟糕的方法 AFAK所以我认为您需要将您的功能更改为类似这样的内容

  @alogintest
Feature: SpecFlowFeature1
try many user logins

Scenario Outline: User login with different product access
Given I am in <productType> login page
And the user is <user>
When I login
Then I should see <prouctResult> logo in my account
Examples:
| productType | userAccount | userPwd | productTypeResult |
| P1 | u1 | pwd1 | p1 |
| P2 | u2 | pwd2 | p2 |

是否可以获取 productUrl 的 FeatureContext 值并在步骤中设置?是的,您可以为每个特定的 productTypeUrl 添加一列,例如

注意您可以添加任意数量的场景

我认为在这种情况下 Given 的正则表达式是不正确的。有没有另一种方法来构造它以将变量作为数据输入的一部分?

是的,因为占位符仅用于 Scenario Outlines

这里生成了你的步骤

using TechTalk.SpecFlow;

namespace loginTest
{
[Binding]
public class SpecFlowFeature1Steps
{
[Given(@"I am in P(.*) login page")]
public void GivenIAmInPLoginPage(int p0)
{
ScenarioContext.Current.Pending();
}

[Given(@"the user is (.*)")]
public void GivenTheUserIs(string p0)
{
ScenarioContext.Current.Pending();
}

[When(@"I login")]
public void WhenILogin()
{
ScenarioContext.Current.Pending();
}

[Then(@"I should see (.*) logo in my account")]
public void ThenIShouldSeeLogoInMyAccount(string p0)
{
ScenarioContext.Current.Pending();
}
}
}

关于c# - 是否可以在 beforeFeature Hook 中使用变量作为 Specflow 场景语句的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43755451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com