【问题标题】:Specflow hooks, how to get Inconclusive or Pending result?Specflow 挂钩,如何获得 Inconclusive 或 Pending 结果?
【发布时间】:2017-03-31 00:28:55
【问题描述】:

我无法在 [AfterScenario] 挂钩中检测到不确定的测试结果。

我有一大套 Specflow 测试,我在大多数晚上运行,在 hooks 部分我记录测试是通过还是失败以及标签上的一些信息,然后在测试运行结束时我输出这个到一个文件。 我目前正在通过以下方式决定测试是通过还是失败:

bool failed = ScenarioContext.Current.TestError != null;
string result = failed ? "failed" : "passed";

这在大多数情况下都有效,但是当测试没有完成所有步骤(场景结果不确定)时,该方法会将场景报告为通过,这并不是我真正想要的。我已经尝试在 App.Config 中将 missingOrPendingStepsOutcome 设置为错误或忽略,但它们都对 TestError 属性没有任何影响,因此它再次被计算为“通过”。

我注意到 ScenarioContext.Current 中有几个看起来很方便的属性( MissingSteps 和 PendingSteps ),不幸的是它们是私有的,所以我无法访问它们。

我将 C#.4.5.2 和 Specflow 1.9.0.77 与 NUnit 2.6.4.14350 一起使用,并在 Windows 7 x64 上 Visual Studio Enterprise 2015 的 ReSharper 9.2 的单元测试会话窗口中运行测试

【问题讨论】:

    标签: c# nunit automated-tests specflow


    【解决方案1】:

    @Florent B. 解决方案更好,但如果您不想通过反思来做到这一点,您可以使用我在 SpecFlow 1.9 中找到的技巧。

    确实,当步骤不存在时,永远不会调用 [BeforeStep] 挂钩。

    [BeforeScenario]
    public void BeforeScenario()
    {
        ScenarioContext.Current.Set(false, "HasNotImplementedStep");
    }
    
    [BeforeStep]
    public void BeforeStep()
    {
        ScenarioContext.Current.Set(true, "IsBeforeStepCalled");
    }
    
    [AfterStep]
    public void AfterStep()
    {
        var beforeStepCalled = ScenarioContext.Current.Get<bool>("IsBeforeStepCalled");
        if (!beforeStepCalled)
        {
            ScenarioContext.Current.Set(true, "HasNotImplementedStep");
        }
    
        ScenarioContext.Current.Set(false, "IsBeforeStepCalled");
    }
    
    [AfterScenario]
    public void AfterScenario()
    {
        var hasNotImplementedStep = ScenarioContext.Current.Get<bool>("HasNotImplementedStep");
        if (hasNotImplementedStep)
        {
            // Do your stuff
        }
    }
    

    【讨论】:

    • 酷,这是一个更好的解决方案,它不需要破解来访问私有变量。
    【解决方案2】:

    您可以检查TestStatus 属性是否不是OK

    bool failed = ScenarioContext.Current.TestStatus != TestStatus.OK;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多