如何填充仅在执行期间生成的未知 Cucumber 或 Specflow 步骤参数?
How to populate unknown Cucumber or Specflow step argument that only be generated during execution?
鉴于我执行了一些步骤,然后在特定步骤中我从数据库单元格中获取了一个值。由于这个值在执行之前是未知的,我不能使用任何绑定或特征文件中定义的 table 值,有没有办法将这个值填充到步骤定义中 => 然后它显示在其他报告中?
例如功能文件:
鉴于我将文件拖放到服务器的 UNC 路径
文件处理成功时
然后加载一个新账户为(.*)(这是运行时生成的数字)
账户只能在最后一步通过连接数据库才能知道,有没有办法把它放到步骤定义中,以便稍后显示为:
新账户加载为 100051359
SpecFlow 做不到你想做的事。但是,您仍然可以从中获得良好的测试,但您可能需要使用 ScenarioContext 在步骤之间共享数据。
处理文件的步骤需要知道新加载的帐户 ID。然后该步骤可以将该帐户 Id 放入 ScenarioContext 中:
[Binding]
public class FileSteps
{
private readonly ScenarioContext scenario;
public FileSteps(ScenarioContext scenario)
{
this.scenario = scenario;
}
[When(@"the file is processed successfully"]
public void WhenTheFileIsProcessedSuccessfully()
{
var account = // process the file
scenario.Set(account.Id, "AccountId");
}
}
稍后进行断言时,在进行断言之前从场景上下文中获取帐户 ID:
[Binding]
public class AccountSteps
{
private readonly ScenarioContext scenario;
public AccountSteps(ScenarioContext scenario)
{
this.scenario = scenario;
}
[Then(@"a new account is loaded")]
public void ThenANewAccountIsLoaded()
{
var account = accountRepository.Find(scenario.Get<int>("AccountId"));
// Assert something about the account
}
}
你的测试变成:
Scenario: ...
Given I drop the file to the server's UNC path
When the file is processed successfully
Then a new account is loaded
鉴于我执行了一些步骤,然后在特定步骤中我从数据库单元格中获取了一个值。由于这个值在执行之前是未知的,我不能使用任何绑定或特征文件中定义的 table 值,有没有办法将这个值填充到步骤定义中 => 然后它显示在其他报告中?
例如功能文件:
鉴于我将文件拖放到服务器的 UNC 路径
文件处理成功时
然后加载一个新账户为(.*)(这是运行时生成的数字)
账户只能在最后一步通过连接数据库才能知道,有没有办法把它放到步骤定义中,以便稍后显示为:
新账户加载为 100051359
SpecFlow 做不到你想做的事。但是,您仍然可以从中获得良好的测试,但您可能需要使用 ScenarioContext 在步骤之间共享数据。
处理文件的步骤需要知道新加载的帐户 ID。然后该步骤可以将该帐户 Id 放入 ScenarioContext 中:
[Binding]
public class FileSteps
{
private readonly ScenarioContext scenario;
public FileSteps(ScenarioContext scenario)
{
this.scenario = scenario;
}
[When(@"the file is processed successfully"]
public void WhenTheFileIsProcessedSuccessfully()
{
var account = // process the file
scenario.Set(account.Id, "AccountId");
}
}
稍后进行断言时,在进行断言之前从场景上下文中获取帐户 ID:
[Binding]
public class AccountSteps
{
private readonly ScenarioContext scenario;
public AccountSteps(ScenarioContext scenario)
{
this.scenario = scenario;
}
[Then(@"a new account is loaded")]
public void ThenANewAccountIsLoaded()
{
var account = accountRepository.Find(scenario.Get<int>("AccountId"));
// Assert something about the account
}
}
你的测试变成:
Scenario: ...
Given I drop the file to the server's UNC path
When the file is processed successfully
Then a new account is loaded