Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Allure.NUnit/Attributes/AllureDisplayIgnoredAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
30 changes: 28 additions & 2 deletions Allure.NUnit/Core/AllureNUnitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal void PrepareTestContext()
internal void StopTestCase()
{
UpdateTestDataFromNUnitProperties();
ApplyDefaultSuiteHierarchy(_test);
AddConsoleOutputAttachment();

var result = TestContext.CurrentContext.Result;
Expand Down Expand Up @@ -224,7 +225,7 @@ static IEnumerable<AllureTestCaseAttribute> 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(
Expand All @@ -235,14 +236,22 @@ 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(
lastDotIndex + 1
);
}

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;
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Label>(l => l.name == LabelName.PARENT_SUITE)
.And.Contains(
Label.Suite("bar")
).UsingPropertiesComparer().And.Contains(
Label.SubSuite("baz")
).UsingPropertiesComparer()
);
}

[TestCase(null)]
[TestCase("")]
public void EmptyOrNullSuiteNotAdded(string suite)
{
TestResult testResult = new() { labels = [] };

ModelFunctions.EnsureSuites(testResult, "foo", suite, "baz");

Assert.That(
testResult.labels,
Does.Contain(
Label.ParentSuite("foo")
).UsingPropertiesComparer()
.And.Exactly(0).Matches<Label>(l => l.name == LabelName.SUITE)
.And.Contains(
Label.SubSuite("baz")
).UsingPropertiesComparer()
);
}

[TestCase(null)]
[TestCase("")]
public void EmptyOrNullSubSuiteNotAdded(string subSuite)
{
TestResult testResult = new() { labels = [] };

ModelFunctions.EnsureSuites(testResult, "foo", "bar", subSuite);

Assert.That(
testResult.labels,
Does.Contain(
Label.ParentSuite("foo")
).UsingPropertiesComparer().And.Contains(
Label.Suite("bar")
).UsingPropertiesComparer().And.Exactly(0).Matches<Label>(
l => l.name == LabelName.SUB_SUITE
)
);
}

[TestCase("parentSuite")]
[TestCase("suite")]
[TestCase("subSuite")]
public void DefaultSuiteLabelsNotAddedIfSuitesHierarchyDefined(string labelName)
{
TestResult testResult = new() { labels = [new() { name = labelName, value = "qux" }] };

ModelFunctions.EnsureSuites(testResult, "foo", "bar", "baz");

Assert.That(
testResult.labels,
Does.Not.Contain(Label.ParentSuite("foo")).UsingPropertiesComparer()
.And.Not.Contains(Label.Suite("bar")).UsingPropertiesComparer()
.And.Not.Contains(Label.SubSuite("baz")).UsingPropertiesComparer()
);
}
}
54 changes: 54 additions & 0 deletions Allure.Net.Commons/Functions/ModelFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,60 @@ e is null
trace = e.ToString()
};

/// <summary>
/// Checks if the test result contains a suite-hierarchy label, i.e., one
/// of the <c>parentSuite</c>, <c>suite</c>, or <c>subSuite</c> labels. If
/// not, adds the provided default values to the list of labels. Otherwise,
/// leaves the test result as is.
/// </summary>
/// <param name="testResult">A test result to modify</param>
/// <param name="parentSuite">
/// A value for the <c>parentSuite</c> label. If null or empty, the label
/// won't be added
/// </param>
/// <param name="suite">
/// A value for the <c>suite</c> label. If null or empty, the label won't
/// be added
/// </param>
/// <param name="subSuite">
/// A value for the <c>subSuite</c> label. If null or empty, the label won't
/// be added
/// </param>
public static void EnsureSuites(
TestResult testResult,
string? parentSuite,
string? suite,
string? subSuite
)
{
var labels = testResult.labels;
if (labels.Any(IsSuiteLabel))
{
return;
}

if (!string.IsNullOrEmpty(parentSuite))
{
labels.Add(Label.ParentSuite(parentSuite));
}

if (!string.IsNullOrEmpty(suite))
{
labels.Add(Label.Suite(suite));
}

if (!string.IsNullOrEmpty(subSuite))
{
labels.Add(Label.SubSuite(subSuite));
}
}

static bool IsSuiteLabel(Label label) => label.name switch
{
LabelName.PARENT_SUITE or LabelName.SUITE or LabelName.SUB_SUITE => true,
_ => false
};

static IEnumerable<string> GetExceptionClassChain(Exception e)
{
for (var type = e.GetType(); type != null; type = type.BaseType)
Expand Down
1 change: 1 addition & 0 deletions Allure.Xunit/AllureMessageSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ void OnTestFinished(MessageHandlerArgs<ITestFinished> args)
this.RunInTestContext(test, () =>
{
this.AddAllureParameters(test, testData.Arguments);
AllureXunitHelper.ApplyDefaultSuites(test.TestCase.TestMethod);
AllureXunitHelper.ReportCurrentTestCase();
if (!IsStaticTestMethod(message))
{
Expand Down
21 changes: 21 additions & 0 deletions Allure.Xunit/AllureXunitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@
});
}

internal static void ApplyDefaultSuites(ITestMethod method)
{
var testClass = method.TestClass.Class;
var runtimeType = testClass.ToRuntimeType();
var assemblyName = runtimeType?.Assembly?.GetName().Name;
var @namespace = runtimeType?.Namespace;
var className =
string.IsNullOrEmpty(@namespace)
? testClass.Name
: testClass.Name?.Substring(@namespace.Length + 1);

Check warning on line 111 in Allure.Xunit/AllureXunitHelper.cs

View workflow job for this annotation

GitHub Actions / Build

Dereference of a possibly null reference.

Check warning on line 111 in Allure.Xunit/AllureXunitHelper.cs

View workflow job for this annotation

GitHub Actions / Build

Dereference of a possibly null reference.

AllureLifecycle.Instance.UpdateTestCase(
testResult => ModelFunctions.EnsureSuites(
testResult,
assemblyName,
@namespace,
className
)
);
}

internal static void ReportCurrentTestCase()
{
AllureLifecycle.Instance.StopTestCase();
Expand Down
Loading