From d5519b58092fb56ae21f473e9d8890a2d5da9ce8 Mon Sep 17 00:00:00 2001 From: Andrey G Date: Fri, 25 Nov 2022 10:11:16 +0300 Subject: [PATCH] Allure.NUnit: fix output attachment --- Allure.NUnit.Examples/OutputTest.cs | 33 ++++++++++++++++++++++++++ Allure.NUnit/Core/AllureNUnitHelper.cs | 9 +++++++ 2 files changed, 42 insertions(+) create mode 100644 Allure.NUnit.Examples/OutputTest.cs diff --git a/Allure.NUnit.Examples/OutputTest.cs b/Allure.NUnit.Examples/OutputTest.cs new file mode 100644 index 00000000..d2d467a0 --- /dev/null +++ b/Allure.NUnit.Examples/OutputTest.cs @@ -0,0 +1,33 @@ +using System; +using System.IO; +using System.Linq; +using Allure.Net.Commons; +using NUnit.Allure.Attributes; +using NUnit.Framework; + +namespace Allure.NUnit.Examples +{ + [AllureSuite("Tests - Output")] + public class OutputTest : BaseTest + { + private const string text = "This should go to console output attachment"; + + [Test] + [Order(1)] + public void WriteOutputTest() + { + Console.WriteLine(text); + } + + [Test] + [Order(2)] + public void OutputLogShouldExist() + { + var resultsDir = AllureLifecycle.Instance.ResultsDirectory; + var attachmentFiles = Directory.EnumerateFiles(resultsDir, "*.txt"); + + Assert.That(attachmentFiles.Any(file => File.ReadAllText(file).Contains(text))); + } + } +} + diff --git a/Allure.NUnit/Core/AllureNUnitHelper.cs b/Allure.NUnit/Core/AllureNUnitHelper.cs index a960db1e..5dc4dc90 100644 --- a/Allure.NUnit/Core/AllureNUnitHelper.cs +++ b/Allure.NUnit/Core/AllureNUnitHelper.cs @@ -85,6 +85,8 @@ private TestFixture GetTestFixture(ITest test) internal void StopTestCase() { UpdateTestDataFromAttributes(); + AddConsoleOutputAttachment(); + for (var i = 0; i < _test.Arguments.Length; i++) { AllureLifecycle.UpdateTestCase(x => x.parameters.Add(new Parameter @@ -176,6 +178,13 @@ private void UpdateTestDataFromAttributes() }); } + private void AddConsoleOutputAttachment() + { + var output = TestExecutionContext.CurrentContext.CurrentResult.Output; + AllureLifecycle.AddAttachment("Console Output", "text/plain", + Encoding.UTF8.GetBytes(output), ".txt"); + } + private IEnumerable GetTestProperties(string name) { var list = new List();