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
52 changes: 46 additions & 6 deletions Allure.Xunit/AllureMessageSink.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Allure.Net.Commons;
using Allure.Net.Commons.TestPlan;
Expand Down Expand Up @@ -73,6 +72,12 @@ void OnTestStarting(MessageHandlerArgs<ITestStarting> args)
{
var message = args.Message;
var test = message.Test;

if (AllureXunitHelper.IsOnExternalAuthority(args.Message.Test))
{
return;
}

var testData = this.GetOrCreateTestData(test);

if (testData.IsSelected)
Expand Down Expand Up @@ -100,7 +105,12 @@ MessageHandlerArgs<ITestClassConstructionFinished> args
{
var message = args.Message;
var test = message.Test;


if (AllureXunitHelper.IsOnExternalAuthority(test))
{
return;
}

if (!IsStaticTestMethod(message))
{
var testResult = this.GetOrCreateTestData(test).TestResult;
Expand All @@ -109,22 +119,46 @@ MessageHandlerArgs<ITestClassConstructionFinished> args
}
}

void OnTestFailed(MessageHandlerArgs<ITestFailed> args) =>
void OnTestFailed(MessageHandlerArgs<ITestFailed> args)
{
var test = args.Message.Test;

if (AllureXunitHelper.IsOnExternalAuthority(test))
{
return;
}

this.RunInTestContext(
args.Message.Test,
test,
() => AllureXunitHelper.ApplyTestFailure(args.Message)
);
}

void OnTestPassed(MessageHandlerArgs<ITestPassed> args)
{
var test = args.Message.Test;

if (AllureXunitHelper.IsOnExternalAuthority(test))
{
return;
}

void OnTestPassed(MessageHandlerArgs<ITestPassed> args) =>
this.RunInTestContext(
args.Message.Test,
test,
() => AllureXunitHelper.ApplyTestSuccess(args.Message)
);
}

void OnTestSkipped(MessageHandlerArgs<ITestSkipped> args)
{
var message = args.Message;
var test = message.Test;

if (AllureXunitHelper.IsOnExternalAuthority(test))
{
return;
}

var testData = this.GetOrCreateTestData(test);
if (testData.IsSelected)
{
Expand All @@ -146,6 +180,12 @@ void OnTestFinished(MessageHandlerArgs<ITestFinished> args)
{
var message = args.Message;
var test = args.Message.Test;

if (AllureXunitHelper.IsOnExternalAuthority(test))
{
return;
}

var testData = this.GetOrCreateTestData(test);

if (testData.IsSelected)
Expand Down
8 changes: 8 additions & 0 deletions Allure.Xunit/AllureXunitConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ namespace Allure.Xunit
{
internal class AllureXunitConfiguration : AllureConfiguration
{
const string KNOWN_TOOL_REQNROLL = "Reqnroll";
const string KNOWN_TOOL_SPECFLOW = "TechTalk.SpecFlow";

public string XunitRunnerReporter { get; set; } = "auto";

public List<string> FirstClassIntegrationTools { get; set; } = [
KNOWN_TOOL_REQNROLL,
KNOWN_TOOL_SPECFLOW,
];

[JsonConstructor]
protected AllureXunitConfiguration(
string? title,
Expand Down
14 changes: 13 additions & 1 deletion Allure.Xunit/AllureXunitHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using Allure.Net.Commons;
Expand All @@ -16,6 +17,17 @@ static class AllureXunitHelper
internal const string NS_OBSOLETE_MSG =
"The Allure.XUnit namespace is deprecated. Please, use Allure.Xunit instead";

/// <summary>
/// Returns <c>true</c> if the test is likely reported by another Allure
/// integration like Allure.Reqnroll.
/// </summary>
internal static bool IsOnExternalAuthority(ITest test) =>
test.TestCase.TestMethod.TestClass.Class
.GetCustomAttributes(typeof(GeneratedCodeAttribute))
.Select(a => a.GetNamedArgument<string>(nameof(GeneratedCodeAttribute.Tool)))
.Where(v => !string.IsNullOrEmpty(v))
.Any(AllureXunitConfiguration.CurrentConfig.FirstClassIntegrationTools.Contains!);

internal static TestResultContainer StartNewAllureContainer(
string className
)
Expand Down Expand Up @@ -108,7 +120,7 @@ internal static void ApplyDefaultSuites(ITestMethod method)
var className =
string.IsNullOrEmpty(@namespace)
? testClass.Name
: testClass.Name?.Substring(@namespace.Length + 1);
: testClass.Name?.Substring(@namespace!.Length + 1);

AllureLifecycle.Instance.UpdateTestCase(
testResult => ModelFunctions.EnsureSuites(
Expand Down
14 changes: 13 additions & 1 deletion Allure.Xunit/AllureXunitPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,24 @@ private static void PatchXunitTestRunnerCtors(Harmony patcher)

private static void OnTestRunnerCreating(ITest test, ref string skipReason)
{
if (AllureXunitHelper.IsOnExternalAuthority(test))
{
return;
}

if (!CurrentSink.SelectByTestPlan(test))
{
skipReason = AllureTestPlan.SkipReason;
}
}

private static void OnTestRunnerCreated(ITest test, object[] testMethodArguments) =>
private static void OnTestRunnerCreated(ITest test, object[] testMethodArguments)
{
if (AllureXunitHelper.IsOnExternalAuthority(test))
{
return;
}

CurrentSink.OnTestArgumentsCreated(test, testMethodArguments);
}
}
9 changes: 9 additions & 0 deletions Allure.Xunit/Schemas/allureConfig.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
"description": "The runner switch or the assembly qualified class name of a runner reporter."
}
]
},
"firstClassIntegrationTools": {
"description": "A list of tools that generate xUnit.net tests and have a tool-specific Allure integration enabled in the project. Allure.Xunit will ignore such tests. The test classes produced by the tools must be marked with `[System.CodeDom.Compiler.GeneratedCodeAttribute]` that mentions the tool as the first argument for this feature to work. The default list includes \"Reqnroll\" and \"TechTalk.SpecFlow\".",
"default": ["Reqnroll", "TechTalk.SpecFlow"],
"type": "array",
"items": {
"type": "string",
"description": "The name of a tool, i.e., the first argument of `[System.CodeDom.Compiler.GeneratedCodeAttribute]` applied by the tool to generated xUnit.net test classes."
}
}
}
}
Expand Down
Loading