Specflow挂钩,如何获得Inconclusive或Pending结果?

时间:2016-08-02 08:20:44

标签: c# nunit automated-tests specflow

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

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

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

这在大多数情况下都有效,但是当测试没有完成所有步骤(场景结果不确定)时,该方法会将场景报告为传递,这实际上并不是我想要的。我已经尝试将missingOrPendingStepsOutcome设置为错误或在App.Config中忽略,但它们都没有对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的单元测试会话窗口中运行测试

2 个答案:

答案 0 :(得分:2)

@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
    }
}

答案 1 :(得分:1)

您可以检查TestStatus属性不是OK

bool failed = ScenarioContext.Current.TestStatus != TestStatus.OK;
相关问题