From d444e22bc5f45fc4c097999630fff62feea1f343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gediminas=20Mila=C5=A1ius?= Date: Thu, 9 May 2024 11:48:08 +0300 Subject: [PATCH] Failing on reqnroll --- .gitignore | 7 + AsyncLocal_ReqNRoll.sln | 22 +++ .../AppServiceProvider.cs | 6 + .../Bindings/MainBindings.cs | 14 ++ .../ExecutionContext.App.Tests.csproj | 35 +++++ .../Features/Test.feature | 4 + .../Features/Test.feature.cs | 132 ++++++++++++++++++ ExecutionContext.App.Tests/GlobalUsings.cs | 1 + ExecutionContext.App.Tests/Startup.cs | 32 +++++ ExecutionContext.App.Tests/UserStartup.cs | 28 ++++ .../ExecutionContext.App.csproj | 13 ++ ExecutionContext.App/MyService.cs | 12 ++ 12 files changed, 306 insertions(+) create mode 100644 .gitignore create mode 100644 AsyncLocal_ReqNRoll.sln create mode 100644 ExecutionContext.App.Tests/AppServiceProvider.cs create mode 100644 ExecutionContext.App.Tests/Bindings/MainBindings.cs create mode 100644 ExecutionContext.App.Tests/ExecutionContext.App.Tests.csproj create mode 100644 ExecutionContext.App.Tests/Features/Test.feature create mode 100644 ExecutionContext.App.Tests/Features/Test.feature.cs create mode 100644 ExecutionContext.App.Tests/GlobalUsings.cs create mode 100644 ExecutionContext.App.Tests/Startup.cs create mode 100644 ExecutionContext.App.Tests/UserStartup.cs create mode 100644 ExecutionContext.App/ExecutionContext.App.csproj create mode 100644 ExecutionContext.App/MyService.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9a011a --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ +.idea +*.Dotsettings.user \ No newline at end of file diff --git a/AsyncLocal_ReqNRoll.sln b/AsyncLocal_ReqNRoll.sln new file mode 100644 index 0000000..08c49f1 --- /dev/null +++ b/AsyncLocal_ReqNRoll.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExecutionContext.App", "ExecutionContext.App\ExecutionContext.App.csproj", "{4B00B6D9-D701-4251-B741-25F47067CB35}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExecutionContext.App.Tests", "ExecutionContext.App.Tests\ExecutionContext.App.Tests.csproj", "{51057BAF-13E9-4736-B614-AC4936BBE288}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4B00B6D9-D701-4251-B741-25F47067CB35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B00B6D9-D701-4251-B741-25F47067CB35}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B00B6D9-D701-4251-B741-25F47067CB35}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B00B6D9-D701-4251-B741-25F47067CB35}.Release|Any CPU.Build.0 = Release|Any CPU + {51057BAF-13E9-4736-B614-AC4936BBE288}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {51057BAF-13E9-4736-B614-AC4936BBE288}.Debug|Any CPU.Build.0 = Debug|Any CPU + {51057BAF-13E9-4736-B614-AC4936BBE288}.Release|Any CPU.ActiveCfg = Release|Any CPU + {51057BAF-13E9-4736-B614-AC4936BBE288}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/ExecutionContext.App.Tests/AppServiceProvider.cs b/ExecutionContext.App.Tests/AppServiceProvider.cs new file mode 100644 index 0000000..ea3a323 --- /dev/null +++ b/ExecutionContext.App.Tests/AppServiceProvider.cs @@ -0,0 +1,6 @@ +namespace ExecutionContext.App.Tests; + +public static class AppServiceProvider +{ + public static IServiceProvider ServiceProvider { get; set; } = null!; +} \ No newline at end of file diff --git a/ExecutionContext.App.Tests/Bindings/MainBindings.cs b/ExecutionContext.App.Tests/Bindings/MainBindings.cs new file mode 100644 index 0000000..595b743 --- /dev/null +++ b/ExecutionContext.App.Tests/Bindings/MainBindings.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; +using Reqnroll; + +namespace ExecutionContext.App.Tests.Bindings; + +[Binding] +public class MainBindings(ScenarioContext scenarioContext) +{ + [When(@"test is run")] + public void WhenTestIsRun() + { + Assert.True(scenarioContext.Get().GetRequiredService().CheckUser("Alice")); + } +} \ No newline at end of file diff --git a/ExecutionContext.App.Tests/ExecutionContext.App.Tests.csproj b/ExecutionContext.App.Tests/ExecutionContext.App.Tests.csproj new file mode 100644 index 0000000..5d54c82 --- /dev/null +++ b/ExecutionContext.App.Tests/ExecutionContext.App.Tests.csproj @@ -0,0 +1,35 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + diff --git a/ExecutionContext.App.Tests/Features/Test.feature b/ExecutionContext.App.Tests/Features/Test.feature new file mode 100644 index 0000000..45575a0 --- /dev/null +++ b/ExecutionContext.App.Tests/Features/Test.feature @@ -0,0 +1,4 @@ +Feature: Test + + Scenario: Run test + When test is run \ No newline at end of file diff --git a/ExecutionContext.App.Tests/Features/Test.feature.cs b/ExecutionContext.App.Tests/Features/Test.feature.cs new file mode 100644 index 0000000..e2be140 --- /dev/null +++ b/ExecutionContext.App.Tests/Features/Test.feature.cs @@ -0,0 +1,132 @@ +// ------------------------------------------------------------------------------ +// +// This code was generated by Reqnroll (https://www.reqnroll.net/). +// Reqnroll Version:1.0.0.0 +// Reqnroll Generator Version:1.0.0.0 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +// ------------------------------------------------------------------------------ +#region Designer generated code +#pragma warning disable +namespace ExecutionContext.App.Tests.Features +{ + using Reqnroll; + using System; + using System.Linq; + + + [System.CodeDom.Compiler.GeneratedCodeAttribute("Reqnroll", "1.0.0.0")] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public partial class TestFeature : object, Xunit.IClassFixture, Xunit.IAsyncLifetime + { + + private static Reqnroll.ITestRunner testRunner; + + private static string[] featureTags = ((string[])(null)); + + private Xunit.Abstractions.ITestOutputHelper _testOutputHelper; + +#line 1 "Test.feature" +#line hidden + + public TestFeature(TestFeature.FixtureData fixtureData, Xunit.Abstractions.ITestOutputHelper testOutputHelper) + { + this._testOutputHelper = testOutputHelper; + } + + public static async System.Threading.Tasks.Task FeatureSetupAsync() + { + testRunner = Reqnroll.TestRunnerManager.GetTestRunnerForAssembly(null, Reqnroll.xUnit.ReqnrollPlugin.XUnitParallelWorkerTracker.Instance.GetWorkerId()); + Reqnroll.FeatureInfo featureInfo = new Reqnroll.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "Features", "Test", null, ProgrammingLanguage.CSharp, featureTags); + await testRunner.OnFeatureStartAsync(featureInfo); + } + + public static async System.Threading.Tasks.Task FeatureTearDownAsync() + { + string testWorkerId = testRunner.TestWorkerId; + await testRunner.OnFeatureEndAsync(); + testRunner = null; + Reqnroll.xUnit.ReqnrollPlugin.XUnitParallelWorkerTracker.Instance.ReleaseWorker(testWorkerId); + } + + public async System.Threading.Tasks.Task TestInitializeAsync() + { + } + + public async System.Threading.Tasks.Task TestTearDownAsync() + { + await testRunner.OnScenarioEndAsync(); + } + + public void ScenarioInitialize(Reqnroll.ScenarioInfo scenarioInfo) + { + testRunner.OnScenarioInitialize(scenarioInfo); + testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs(_testOutputHelper); + } + + public async System.Threading.Tasks.Task ScenarioStartAsync() + { + await testRunner.OnScenarioStartAsync(); + } + + public async System.Threading.Tasks.Task ScenarioCleanupAsync() + { + await testRunner.CollectScenarioErrorsAsync(); + } + + async System.Threading.Tasks.Task Xunit.IAsyncLifetime.InitializeAsync() + { + await this.TestInitializeAsync(); + } + + async System.Threading.Tasks.Task Xunit.IAsyncLifetime.DisposeAsync() + { + await this.TestTearDownAsync(); + } + + [Xunit.SkippableFactAttribute(DisplayName="Run test")] + [Xunit.TraitAttribute("FeatureTitle", "Test")] + [Xunit.TraitAttribute("Description", "Run test")] + public async System.Threading.Tasks.Task RunTest() + { + string[] tagsOfScenario = ((string[])(null)); + System.Collections.Specialized.OrderedDictionary argumentsOfScenario = new System.Collections.Specialized.OrderedDictionary(); + Reqnroll.ScenarioInfo scenarioInfo = new Reqnroll.ScenarioInfo("Run test", null, tagsOfScenario, argumentsOfScenario, featureTags); +#line 3 + this.ScenarioInitialize(scenarioInfo); +#line hidden + if ((TagHelper.ContainsIgnoreTag(tagsOfScenario) || TagHelper.ContainsIgnoreTag(featureTags))) + { + testRunner.SkipScenario(); + } + else + { + await this.ScenarioStartAsync(); +#line 4 + await testRunner.WhenAsync("test is run", ((string)(null)), ((Reqnroll.Table)(null)), "When "); +#line hidden + } + await this.ScenarioCleanupAsync(); + } + + [System.CodeDom.Compiler.GeneratedCodeAttribute("Reqnroll", "1.0.0.0")] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class FixtureData : object, Xunit.IAsyncLifetime + { + + async System.Threading.Tasks.Task Xunit.IAsyncLifetime.InitializeAsync() + { + await TestFeature.FeatureSetupAsync(); + } + + async System.Threading.Tasks.Task Xunit.IAsyncLifetime.DisposeAsync() + { + await TestFeature.FeatureTearDownAsync(); + } + } + } +} +#pragma warning restore +#endregion diff --git a/ExecutionContext.App.Tests/GlobalUsings.cs b/ExecutionContext.App.Tests/GlobalUsings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/ExecutionContext.App.Tests/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/ExecutionContext.App.Tests/Startup.cs b/ExecutionContext.App.Tests/Startup.cs new file mode 100644 index 0000000..46389a1 --- /dev/null +++ b/ExecutionContext.App.Tests/Startup.cs @@ -0,0 +1,32 @@ +using Microsoft.Extensions.DependencyInjection; +using TechTalk.SpecFlow; + +namespace ExecutionContext.App.Tests; + +[Binding] +public class Startup(Reqnroll.ScenarioContext scenarioContext) +{ + [BeforeTestRun] + public static void BeforeTestRun() + { + AppServiceProvider.ServiceProvider = new ServiceCollection() + .AddHttpContextAccessor() + .AddScoped() + .BuildServiceProvider(); + } + + [BeforeScenario(Order = BeforeTestRunAttribute.DefaultOrder - 1000)] + public void BeforeScenario() + { + var scope = AppServiceProvider.ServiceProvider.CreateScope(); + + scenarioContext.Set(scope); + scenarioContext.Set(scope.ServiceProvider); + } + + [AfterScenario(Order = BeforeTestRunAttribute.DefaultOrder + 1000)] + public void AfterScenario() + { + scenarioContext.Get().Dispose(); + } +} \ No newline at end of file diff --git a/ExecutionContext.App.Tests/UserStartup.cs b/ExecutionContext.App.Tests/UserStartup.cs new file mode 100644 index 0000000..2b9501e --- /dev/null +++ b/ExecutionContext.App.Tests/UserStartup.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using TechTalk.SpecFlow; + +namespace ExecutionContext.App.Tests; + +[Binding] +public class UserStartup(Reqnroll.ScenarioContext scenarioContext) +{ + [BeforeScenario] + public void BeforeScenario() + { + var accessor = scenarioContext.Get().GetRequiredService(); + + // Prepopulate context with some default values. + accessor.HttpContext = new DefaultHttpContext() + { + Request = + { + Headers = + { + ["UserId"] = "12345", + ["UserName"] = "Alice" + } + } + }; + } +} \ No newline at end of file diff --git a/ExecutionContext.App/ExecutionContext.App.csproj b/ExecutionContext.App/ExecutionContext.App.csproj new file mode 100644 index 0000000..d86dcb5 --- /dev/null +++ b/ExecutionContext.App/ExecutionContext.App.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/ExecutionContext.App/MyService.cs b/ExecutionContext.App/MyService.cs new file mode 100644 index 0000000..2f97558 --- /dev/null +++ b/ExecutionContext.App/MyService.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Http; + +namespace ExecutionContext.App; + +public class MyService(IHttpContextAccessor accessor) +{ + public bool CheckUser(string username) + { + var context = accessor.HttpContext ?? throw new InvalidOperationException("Context Should not be null here"); + return context.Request.Headers["UserName"] == username; + } +} \ No newline at end of file