Allure.XUnit icon indicating copy to clipboard operation
Allure.XUnit copied to clipboard

Allure reports are not generated if add dependency "Xunit.dependencyInjection(7.2.0)" to project packages

Open romart333 opened this issue 3 years ago • 3 comments

If add Xunit.dependencyInjection nugget package and Startup class to .NET project then allure reports stop being created. I suppose it's a matter of changing the entry point to the application

Screenshot_2 png

romart333 avatar Jun 02 '21 08:06 romart333

Hello. Thank your for issue report, @romart333 . We will think out on this issue, and come with our feedback.

Faithfully yours, Ivan

IvanWR1995 avatar Sep 10 '21 14:09 IvanWR1995

Team, any timeline on this? Still not able to use Allure reports with Xunit dependency injection. Please look into this.

tkeerthivel avatar Apr 28 '22 09:04 tkeerthivel

It looks like the issue is related to Xunit.DependencyInjection itself. Refer to the similar issue https://github.com/pengweiqhca/Xunit.DependencyInjection/issues/46. As a workaround, it should be possible to define a custom Xunit.DependencyInjection.IXunitTestCaseRunnerWrapper and duplicate the logic of the AllureXunitTestCase.RunAsync method in it:

using Allure.Xunit;
using Allure.XUnit;
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Xunit.DependencyInjection.AllureXUnit
{
    public class AllureXunitTestCaseRunnerWrapper : DependencyInjectionTestCaseRunnerWrapper
    {
        private static readonly Type testCaseType;
        private static readonly PropertyInfo testResultAccessor;
        static AllureXunitTestCaseRunnerWrapper()
        {
            testCaseType = typeof(Steps).Assembly.GetType("Allure.XUnit.AllureXunitTestCase");
            testResultAccessor = typeof(Steps).GetProperty("TestResultAccessor", BindingFlags.NonPublic | BindingFlags.Static);
        }
        public override Type TestCaseType => testCaseType;
        public override Task<RunSummary> RunAsync(IXunitTestCase testCase, IServiceProvider provider,
            IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments,
            ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            testResultAccessor.SetValue(default, testCase);
            messageBus = new AllureMessageBus(messageBus);
            return base.RunAsync(testCase, provider, diagnosticMessageSink, messageBus,
                constructorArguments, aggregator, cancellationTokenSource);
        }
    }
}

Then register it as a service:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Xunit.DependencyInjection;
using Xunit.DependencyInjection.AllureXUnit;

namespace Allure.XUnit.Examples
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.TryAddEnumerable(ServiceDescriptor.Singleton<IXunitTestCaseRunnerWrapper, AllureXunitTestCaseRunnerWrapper>());
        }
    }
}

pos777 avatar May 26 '22 06:05 pos777