【问题标题】:Quit a specflow scenario, or avoid execution based on a precondition退出 specflow 场景,或避免基于前提条件执行
【发布时间】:2021-10-26 12:15:28
【问题描述】:

我希望能够避免执行 specflow 场景,或者在第一步退出,具体取决于外部配置。这可能吗?

喜欢 背景: 假设我们有一个满足的前提条件

前提条件失败,场景停止且不返回错误

【问题讨论】:

    标签: specflow execution


    【解决方案1】:

    IUnitTestRuntimeProvider 接口应该被注入 https://docs.specflow.org/projects/specflow/en/latest/Execution/SkippingScenarios.html

    然而,我又遇到了这个界面无法解析的事实 https://github.com/SpecFlowOSS/SpecFlow/issues/2076 在带有 Visual Studio 2019 的 .Net 5、XUnit 项目中,此解决方案对我不起作用

    【讨论】:

    • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
    【解决方案2】:

    如果某个步骤未引发异常,则该步骤通过。您可以将该特定步骤重构为私有方法,然后简单地将方法调用包装在 if 语句中的 try-catch 中:

    [Given(@"we have a satisfied precondition")]
    public void GivenWeHaveASatisfiedPrecondition()
    {
       if (/* check your external config here */)
       {
          try
          {
             PerformPrecondition();
          }
          catch (Exception ex)
          {
             Console.WriteLine("Step failed, but continuing scenario." + Environment.NewLine + Environment.NewLine + ex);
          }
       }
       else
       {
          // Failure in this step should fail the scenario.
          PerformPrecondition();
       }
    }
    
    private void PerformPrecondition()
    {
       // Your step logic goes here
    }
    

    您尚未指定外部配置的位置,但有上千种方法可以做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 2016-07-13
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      相关资源
      最近更新 更多