【问题标题】:How to add attachments to Allure report in C#?如何在 C# 中向 Allure 报告添加附件?
【发布时间】:2018-10-18 10:19:32
【问题描述】:

Allure framework 是一个非常漂亮的测试报告框架。 然而,它的 C# 文档相当糟糕。

我想在我的魅力报告中添加一些内容:

  • 调试日志(就像我为调试而写的所有内容)
  • 截图
  • 一个文件

怎么做?我不知道,如果你知道怎么做,请帮助我。 AllureLifecycle class 似乎可以帮助我,但我不知道如何使用它。

如果重要,我将 Allure 与 SpecFlowMS 测试 一起使用。

【问题讨论】:

    标签: c# specflow allure


    【解决方案1】:

    我搜索了更多,似乎找到了真相。

    事实上,可以添加我想要的所有附件,但它们只能作为文件添加:

    byte[] log = Encoding.ASCII.GetBytes(Log.GetAllLog());
    AllureLifecycle.Instance.AddAttachment("DebugLog", "application/json", log, "json");
    

    如果您想从实际路径(位置)添加文件,您可以使用相同的方法但不同的重载。

    因此,只需将此代码放在“teardown\afterscenario”方法或您要制作此附件的任何其他位置(例如“afterstep”方法)。我使用 SpecFlow,所以如果我将它添加到“AfterStep”钩子中,那么 Allure 会显示附加到特定步骤的那些文件!太棒了!)

    【讨论】:

      【解决方案2】:

      似乎 allure 有一些可以使用的事件。 请参阅:https://github.com/allure-framework/allure-csharp-commons/blob/master/AllureCSharpCommons.Tests/IntegrationTests.cs 了解更多信息。

      我自己没有尝试过,但是根据文档,这样的东西应该可以工作。

        _lifecycle = Allure.DefaultLifecycle;  
        _lifecycle.Fire(new
       MakeAttachmentEvent(AllureResultsUtils.TakeScreenShot(),
                       "Screenshot",
                       "image/png"));
       _lifecycle.Fire(new MakeAttachmentEvent(File.ReadAllBytes("TestData/attachment.json"),
                  "JsonAttachment",
                  "application/json"));
      

      希望这会有所帮助。

      【讨论】:

      • 感谢此链接,我正在寻找它!希望对我有帮助。
      • 它不起作用。我无法使用此示例添加附件(
      • 你得到了什么结果?
      • 实际上什么都没有 - 只是没有附上屏幕截图。
      【解决方案3】:

      在 AfterScenario 方法中使用这种代码:

      if (_scenarioContext.TestError != null)
                  {
                      var path = WebElementsUtils.MakeScreenshot(_driver);
                      _allureLifecycle.AddAttachment(path);
                  }
      

      首先它验证,如果场景通过,如果没有,那么

      WebElementsUtils.MakeScreenshot(_driver)

      方法制作截图并返回它的路径。然后我给 Allure 的这条路。作为同一方法中的第二个参数,我可以给出屏幕截图的名称。结果,我在 Allure 报告的 AfterScenario 块中获得了屏幕截图。 附言这只是截图,关于日志不能说明什么。

      【讨论】:

        【解决方案4】:

        通过此示例,您可以将附件完全添加到失败的步骤

                [AfterStep(Order = 0)]
                public void RecordScreenFailure(ScenarioContext scenarioContext)
                {
                    if (scenarioContext.TestError != null)
                    {
                        Allure.Commons.AllureLifecycle allureInstance = Allure.Commons.AllureLifecycle.Instance;
                        string screenshotPath = MagicMethodMakingScreenshotAndReturningPathToIt(); 
                        allureInstance.UpdateTestCase(testResult => {
                            Allure.Commons.StepResult failedStepRsult = 
                                testResult.steps.First(step => step.status == Allure.Commons.Status.failed);
                            failedStepRsult.attachments.Add(new Allure.Commons.Attachment() { 
                                name = "failure screen", 
                                source = screenshotPath, 
                                type = "image/png"
                            });
        
                        });
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-08-17
          • 1970-01-01
          • 2022-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多