如何在C#中向“魅力”报告添加附件?

时间:2018-10-18 10:19:33

标签: c# specflow allure

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

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

  • 调试日志(就像我写的所有要调试的东西一样)
  • 屏幕截图
  • 文件

该怎么做?我不知道,如果您知道该怎么做,请帮助我。似乎var dateEncodedPropertyName = "System.Media.DateEncoded"; var propertyNames = new List<string>() { dateEncodedPropertyName }; // Get extended properties IDictionary<string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertyNames); // Get the property value var propValue = extraProperties[dateEncodedPropertyName]; if (propValue != null) { Debug.WriteLine(propValue); } 类可以帮助我,但我不确定如何使用它。

如果有问题,我将Allure与 SpecFlow MS测试一起使用。

4 个答案:

答案 0 :(得分:1)

看来,魅力具有一些可以使用的事件。 有关更多信息,请参见: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"));

希望这会有所帮助。

答案 1 :(得分:1)

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

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

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

如果要从实际路径(位置)添加文件,则可以使用相同的方法来执行,但是要使用不同的重载。

因此,只需将此代码放在“拆解\后场景”方法中或要进行此附件的任何其他位置(例如,在“后步”方法中)。我使用SpecFlow,所以如果将其添加到“ AfterStep”挂钩中,则Allure将显示附加到特定步骤的那些文件!太神奇了!)

答案 2 :(得分:0)

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

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

首先,如果通过方案,它会进行验证;否则,请进行验证

  

WebElementsUtils.MakeScreenshot(_driver)

方法制作屏幕截图并返回其路径。然后这条路我给魅力。作为相同方法中的第二个参数,我可以给出屏幕截图的名称。结果,我在“魅力”报告的AfterScenario块中获得了屏幕截图。 附言这仅用于屏幕截图,关于日志无法说明任何内容。

答案 3 :(得分:0)

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

        [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"
                    });

                });
            }
        }
相关问题