Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add attachments to Allure report in C#?

The Allure framework is a really beautiful framework for test reporting. Yet it has rather bad documentation for C#.

I want to add some things to my allure report:

  • Debug log (like all things I write to debug)
  • Screenshot
  • A file

How to do it? I have no idea, please help me if you know how to do it. It seems like AllureLifecycle class can help me but I'm not sure how to use it.

In case it matters I use Allure together with SpecFlow and MS test.

like image 200
Denis Koreyba Avatar asked Oct 30 '25 11:10

Denis Koreyba


2 Answers

I searched more and seems I found the Truth.

And the Truth is it's possible to add all attachments I wanted but they can be added only as a file:

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

If you want to add a file from actually a path (location) you can do it with the same method but a different overload.

So just place this code in a "teardown\afterscenario" method or at any other place (for example at "afterstep" method) where you want to make this attachment. I use SpecFlow so if I add this to "AfterStep" hook then Allure displays those files attached to a specific step! That's amazing!)

like image 73
Denis Koreyba Avatar answered Nov 02 '25 01:11

Denis Koreyba


it seems that allure has some events that can be used. See : https://github.com/allure-framework/allure-csharp-commons/blob/master/AllureCSharpCommons.Tests/IntegrationTests.cs for more information.

haven't tried it myself, but something like this should work according to the documentation.

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

Hope this helps.

like image 27
Jeroen Lamberts Avatar answered Nov 02 '25 01:11

Jeroen Lamberts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!