Skip to content

Commit

Permalink
NUnit - Add an ability to save OneTimeSetUp step results (#333)
Browse files Browse the repository at this point in the history
* Move common steps

* save onetime setup result

* add random

* skip adding empty onetimesetup result
  • Loading branch information
alekskulakov authored Mar 14, 2023
1 parent 523118b commit 2d62d65
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 36 deletions.
42 changes: 42 additions & 0 deletions Allure.NUnit.Examples/AllureAsyncOneTimeSetUpTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading.Tasks;
using Allure.NUnit.Examples.CommonSteps;
using NUnit.Allure.Attributes;
using NUnit.Framework;

namespace Allure.NUnit.Examples
{
[AllureSuite("Tests - Async OneTime SetUp")]
[Parallelizable(ParallelScope.All)]
public class AllureAsyncOneTimeSetUpTests: BaseTest
{
[OneTimeSetUp]
public async Task OneTimeSetUp()
{
await AsyncStepsExamples.PrepareDough();
await AsyncStepsExamples.CookPizza();
await AsyncStepsExamples.CookPizza();
await AsyncStepsExamples.CookPizza();
}

[SetUp]
public async Task SetUp()
{
await AsyncStepsExamples.PrepareDough();
}

[Test]
[AllureName("Test1")]
public async Task Test1()
{
await AsyncStepsExamples.DeliverPizza();
await AsyncStepsExamples.Pay();
}

[Test]
[AllureName("Test2")]
public async Task Test2()
{
await AsyncStepsExamples.DeliverPizza();
}
}
}
22 changes: 1 addition & 21 deletions Allure.NUnit.Examples/AllureAsyncStepTest.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
using System;
using System.Threading.Tasks;
using Allure.NUnit.Examples.CommonSteps;
using NUnit.Allure.Attributes;
using NUnit.Framework;

namespace Allure.NUnit.Examples;

public static class AsyncStepsExamples
{
[AllureStep("Prepare dough")]
public static async Task PrepareDough()
{
await AsyncMethod($"Step {nameof(PrepareDough)}");
}

[AllureStep("Cook pizza")]
public static async Task CookPizza()
{
await AsyncMethod($"Step {nameof(CookPizza)}");
}
private static async Task AsyncMethod(string message)
{
await Task.Delay(3);
Console.WriteLine($"{message}");
}
}

[AllureSuite("Tests - Async Steps")]
public class AllureAsyncStepTest : BaseTest
{
Expand Down
40 changes: 40 additions & 0 deletions Allure.NUnit.Examples/CommonSteps/AsyncStepsExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
using NUnit.Allure.Attributes;

namespace Allure.NUnit.Examples.CommonSteps;

public static class AsyncStepsExamples
{
private static readonly Random Random = new();

[AllureStep("Prepare dough")]
public static async Task PrepareDough()
{
await AsyncMethod($"Step {nameof(PrepareDough)}");
}

[AllureStep("Cook pizza")]
public static async Task CookPizza()
{
await AsyncMethod($"Step {nameof(CookPizza)}");
}

[AllureStep("Deliver")]
public static async Task DeliverPizza()
{
await AsyncMethod($"Step {nameof(DeliverPizza)}");
}

[AllureStep("Pay")]
public static async Task Pay()
{
await AsyncMethod($"Step {nameof(Pay)}");
}
private static async Task AsyncMethod(string message)
{
var delay = Random.Next(50, 200);
await Task.Delay(delay);
Console.WriteLine($"{message}");
}
}
33 changes: 28 additions & 5 deletions Allure.NUnit/Core/AllureNUnitAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using Allure.Net.Commons;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;

namespace NUnit.Allure.Core
{
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class)]
public class AllureNUnitAttribute : PropertyAttribute, ITestAction
public class AllureNUnitAttribute : PropertyAttribute, ITestAction, IApplyToContext
{
private readonly ConcurrentDictionary<string, AllureNUnitHelper> _allureNUnitHelper = new ConcurrentDictionary<string, AllureNUnitHelper>();
private readonly bool _isWrappedIntoStep;
Expand All @@ -34,19 +35,41 @@ public void BeforeTest(ITest test)
{
var helper = new AllureNUnitHelper(test);
_allureNUnitHelper.AddOrUpdate(test.Id, helper, (key, existing) => helper);
helper.StartTestContainer();
helper.StartTestCase();

if (test.IsSuite)
{
helper.SaveOneTimeResultToContext();
StepsHelper.StopFixture();
}
else
{
helper.StartTestContainer();
helper.AddOneTimeSetupResult();
helper.StartTestCase();
}
}

public void AfterTest(ITest test)
{
if (_allureNUnitHelper.TryGetValue(test.Id, out var helper))
{
helper.StopTestCase();
if (!test.IsSuite)
{
helper.StopTestCase();
}

helper.StopTestContainer();
}
}

public ActionTargets Targets => ActionTargets.Test;
public ActionTargets Targets => ActionTargets.Test | ActionTargets.Suite;

public void ApplyToContext(TestExecutionContext context)
{
var test = context.CurrentTest;
var helper = new AllureNUnitHelper(test);
helper.StartTestContainer();
StepsHelper.StartBeforeFixture($"fr-{test.Id}");
}
}
}
55 changes: 48 additions & 7 deletions Allure.NUnit/Core/AllureNUnitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ public AllureNUnitHelper(ITest test)
internal void StartTestContainer()
{
StepsHelper.TestResultAccessor = this;
var testFixture = GetTestFixture(_test);
_containerGuid = string.Concat(Guid.NewGuid().ToString(), "-tc-", testFixture.Id);
TestResultContainer = new TestResultContainer
{
uuid = _containerGuid,
uuid = ContainerId,
name = _test.FullName
};
AllureLifecycle.StartTestContainer(TestResultContainer);
Expand All @@ -65,7 +63,7 @@ internal void StartTestCase()
Label.TestClass(_test.ClassName?.Substring(_test.ClassName.LastIndexOf('.') + 1))
}
};
AllureLifecycle.StartTestCase(_containerGuid, TestResult);
AllureLifecycle.StartTestCase(ContainerId, TestResult);
}

private TestFixture GetTestFixture(ITest test)
Expand Down Expand Up @@ -112,8 +110,8 @@ internal void StopTestCase()

internal void StopTestContainer()
{
AllureLifecycle.StopTestContainer(_containerGuid);
AllureLifecycle.WriteTestContainer(_containerGuid);
AllureLifecycle.StopTestContainer(ContainerId);
AllureLifecycle.WriteTestContainer(ContainerId);
}

public static Status GetNUnitStatus()
Expand Down Expand Up @@ -157,7 +155,6 @@ public static Status GetNUnitStatus()
return Status.passed;
}


private void UpdateTestDataFromAttributes()
{
foreach (var p in GetTestProperties(PropertyNames.Description))
Expand Down Expand Up @@ -207,5 +204,49 @@ public void WrapInStep(Action action, string stepName = "")
{
AllureLifecycle.WrapInStep(action, stepName);
}

private string ContainerId => $"tc-{_test.Id}";

public void SaveOneTimeResultToContext()
{
var currentResult = TestExecutionContext.CurrentContext.CurrentResult;

if (!string.IsNullOrEmpty(currentResult.Output))
{
AllureLifecycle.Instance.AddAttachment("Console Output", "text/plain",
Encoding.UTF8.GetBytes(currentResult.Output), ".txt");
}

FixtureResult fixtureResult = null;
AllureLifecycle.Instance.UpdateFixture(fr =>
{
fr.name = "OneTimeSetUp";
fr.status = fr.steps.SelectMany(s => s.steps)
.All(s => s.status == Status.passed)
? Status.passed
: Status.failed;

fixtureResult = fr;
});

var testFixture = GetTestFixture(TestExecutionContext.CurrentContext.CurrentTest);
testFixture.Properties.Set("OneTimeSetUpResult", fixtureResult);
}

public void AddOneTimeSetupResult()
{
var testFixture = GetTestFixture(TestExecutionContext.CurrentContext.CurrentTest);
FixtureResult fixtureResult = null;

fixtureResult = testFixture.Properties.Get("OneTimeSetUpResult") as FixtureResult;

if (fixtureResult != null && fixtureResult.steps.Any())
{
AllureLifecycle.UpdateTestContainer(TestResultContainer.uuid, container =>
{
container.befores.Add(fixtureResult);
});
}
}
}
}
5 changes: 2 additions & 3 deletions Allure.Net.Commons/Storage/AllureStorage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace Allure.Net.Commons.Storage
{
Expand Down Expand Up @@ -35,6 +33,7 @@ public T Remove<T>(string uuid)
public void ClearStepContext()
{
Steps.Clear();
stepContext.TryRemove(AllureLifecycle.CurrentTestIdGetter(), out _);
}

public void StartStep(string uuid)
Expand Down

0 comments on commit 2d62d65

Please sign in to comment.