【问题标题】:Specflow - [BeforeTestRun] and [AfterTestRun] are not being executedspecflow - [beforoteStrun]和[后叉胸部]没有被执行
【发布时间】:2021-01-04 05:47:27
【问题描述】:

我的[BeforeTestRun][AfterTestRun] 绑定没有被执行,
[BeforeScenario][AfterStep][AfterScenario]正在执行。

从这个类中执行的第一个绑定是[BeforeScenario]。我有点不知道为什么 [BeforeTestRun][AfterTestRun] 被忽略了。

带有BeforeTestRunAttribute的代码

[BeforeTestRun]
public static void TestInitialize()
{
    var htmlReporter = new ExtentHtmlReporter(@"D:\\ExtentReport\\");
    extent = new AventStack.ExtentReports.ExtentReports();
    extent.AttachReporter(htmlReporter);
}

代码为AfterTestRunAttribute

[AfterTestRun]
public static void CleanUp()
{
    extent.Flush();
}

在执行此代码([AfterStep][BeforeScenario])时:

代码为BeforeScenarioAttribute

[BeforeScenario]
public void BeforeScenario()
{
    InitializeSettings();
    featureName = extent.CreateTest<Feature>(_featureContext.FeatureInfo.Title);
    scenario = featureName.CreateNode<Scenario>(_scenarioContext.ScenarioInfo.Title);
}

代码为AfterStepAttribute

[AfterStep]
public void InsertReportingSteps()
{
    var stepName = _scenarioContext.StepContext.StepInfo.Text;
    var featureName = _featureContext.FeatureInfo.Title;
    var scenarioName = _scenarioContext.ScenarioInfo.Title;

    var stepType = _scenarioContext.StepContext.StepInfo.StepDefinitionType.ToString();

    if (_scenarioContext.TestError == null)
        .....[etc..]
}

有人知道为什么我的BeforeTestRunAfterTestRun 绑定没有被执行吗?
所有绑定都在同一个HookInitialize.cs 类中。

  • Specflow 版本 3.4.8

  • Specflow.NUnit 版本 3.4.8

  • NUnit 版本 3.12.0

  • NUnit3TestAdapter 版本 3.17.0

【问题讨论】:

  • 您是否将某处的 MSBuild 属性 GenerateSpecFlowAssemblyHooksFile 设置为 false?
  • 感谢您的回复,但没有。我的 *.csproj 文件中没有提到
  • 您能创建一个问题可重现的小项目吗? MSbuild 属性是一个快速的想法,但没有更多信息,我无法说出原因。
  • 嗯,它是一个相当小的框架,所以在这个意义上它并不是现在已经很大了。唯一的问题是还添加了并行执行等功能。但据我所知,这不应该影响这部分。
  • 您最终解决了这个问题吗?在重构我们的解决方案后遇到同样的问题(所以它曾经可以工作)但不知道为什么......

标签: c# testing binding automated-tests specflow


【解决方案1】:

Nunit 使用 [OneTimeSetup]、[OneTimeTearDown] 等可能会检查一下。我对规范流的了解非常有限,但总的来说,如果可能的话,我会坚持使用一个框架的绑定。

【讨论】:

  • 您没有在 SpecFlow 中使用 NUnit 属性。我们为此提供了挂钩,可以集成到所有不同的测试运行器中。使用此属性会破坏 SpecFlow 运行时中的内容。不要使用它们!
猜你喜欢
  • 1970-01-01
  • 2016-05-02
  • 2022-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 2014-04-22
相关资源
最近更新 更多