diff --git a/Allure.NUnit/Attributes/AllureDisplayIgnoredAttribute.cs b/Allure.NUnit/Attributes/AllureDisplayIgnoredAttribute.cs index fa961371..a32b9106 100644 --- a/Allure.NUnit/Attributes/AllureDisplayIgnoredAttribute.cs +++ b/Allure.NUnit/Attributes/AllureDisplayIgnoredAttribute.cs @@ -84,6 +84,9 @@ void EmitTestResult(ITest test, TestResult testResult) this.ApplyLegacySuiteLabels(testResult, reason); AllureLifecycle.Instance.StartTestCase(testResult); + + AllureNUnitHelper.ApplyDefaultSuiteHierarchy(test); + AllureLifecycle.Instance.StopTestCase(); AllureLifecycle.Instance.WriteTestCase(); } diff --git a/Allure.NUnit/Core/AllureNUnitHelper.cs b/Allure.NUnit/Core/AllureNUnitHelper.cs index 911f38df..68435fdb 100644 --- a/Allure.NUnit/Core/AllureNUnitHelper.cs +++ b/Allure.NUnit/Core/AllureNUnitHelper.cs @@ -58,6 +58,7 @@ internal void PrepareTestContext() internal void StopTestCase() { UpdateTestDataFromNUnitProperties(); + ApplyDefaultSuiteHierarchy(_test); AddConsoleOutputAttachment(); var result = TestContext.CurrentContext.Result; @@ -224,7 +225,7 @@ static IEnumerable IterateAllAllureAttribites(ITest tes static string GetNamespace(string classFullName) { - var lastDotIndex = classFullName?.LastIndexOf('.') ?? -1; + var lastDotIndex = StripTypeArgs(classFullName)?.LastIndexOf('.') ?? -1; return lastDotIndex == -1 ? null : classFullName.Substring( @@ -235,7 +236,7 @@ static string GetNamespace(string classFullName) static string GetClassName(string classFullName) { - var lastDotIndex = classFullName?.LastIndexOf('.') ?? -1; + var lastDotIndex = StripTypeArgs(classFullName)?.LastIndexOf('.') ?? -1; return lastDotIndex == -1 ? classFullName : classFullName.Substring( @@ -243,6 +244,14 @@ static string GetClassName(string classFullName) ); } + static string StripTypeArgs(string classFullName) + { + var typeArgsStart = classFullName?.IndexOf('<') ?? -1; + return typeArgsStart == -1 + ? classFullName + : classFullName.Substring(0, typeArgsStart); + } + static TestFixture GetTestFixture(ITest test) { var currentTest = test; @@ -263,6 +272,23 @@ static TestFixture GetTestFixture(ITest test) ); } + internal static void ApplyDefaultSuiteHierarchy(ITest test) + { + var testClassFullName = GetTestFixture(test).FullName; + var assemblyName = test.TypeInfo?.Assembly?.GetName().Name; + var @namespace = GetNamespace(testClassFullName); + var className = GetClassName(testClassFullName); + + AllureLifecycle.UpdateTestCase( + testResult => ModelFunctions.EnsureSuites( + testResult, + assemblyName, + @namespace, + className + ) + ); + } + private void UpdateTestDataFromNUnitProperties() { foreach (var p in GetTestProperties(PropertyNames.Description)) diff --git a/Allure.Net.Commons.Tests/FunctionTests/ModelFunctionTests/DefaultSuiteTests.cs b/Allure.Net.Commons.Tests/FunctionTests/ModelFunctionTests/DefaultSuiteTests.cs new file mode 100644 index 00000000..9b067ac9 --- /dev/null +++ b/Allure.Net.Commons.Tests/FunctionTests/ModelFunctionTests/DefaultSuiteTests.cs @@ -0,0 +1,102 @@ +using NUnit.Framework; +using Allure.Net.Commons.Functions; + +namespace Allure.Net.Commons.Tests.FunctionTests.ModelFunctionTests; + +class DefaultSuiteTests +{ + [Test] + public void DefaultSuiteLabelsAdded() + { + TestResult testResult = new() { labels = [] }; + + ModelFunctions.EnsureSuites(testResult, "foo", "bar", "baz"); + + Assert.That( + testResult.labels, + Does.Contain( + Label.ParentSuite("foo") + ).UsingPropertiesComparer().And.Contains( + Label.Suite("bar") + ).UsingPropertiesComparer().And.Contains( + Label.SubSuite("baz") + ).UsingPropertiesComparer() + ); + } + + [TestCase(null)] + [TestCase("")] + public void EmptyOrNullParentSuiteNotAdded(string parentSuite) + { + TestResult testResult = new() { labels = [] }; + + ModelFunctions.EnsureSuites(testResult, parentSuite, "bar", "baz"); + + Assert.That( + testResult.labels, + Has.Exactly(0).Matches