From c4880d70c5d27eb3f04c465d04a0e8a64e23bdac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1sp=C3=A1r=20Nagy?= Date: Mon, 7 Oct 2024 11:05:38 +0200 Subject: [PATCH] Support new execution structure for xUnit --- .../Generation/UnitTestFeatureGenerator.cs | 15 ++++----------- .../XUnit2TestGeneratorProvider.cs | 12 +++++++++++- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Reqnroll.Generator/Generation/UnitTestFeatureGenerator.cs b/Reqnroll.Generator/Generation/UnitTestFeatureGenerator.cs index 2bfb43c75..7095f2ef7 100644 --- a/Reqnroll.Generator/Generation/UnitTestFeatureGenerator.cs +++ b/Reqnroll.Generator/Generation/UnitTestFeatureGenerator.cs @@ -174,6 +174,7 @@ private void SetupTestClassInitializeMethod(TestClassGenerationContext generatio _testGeneratorProvider.SetTestClassInitializeMethod(generationContext); + /* //testRunner = TestRunnerManager.GetTestRunnerForAssembly(null, [test_worker_id]); var testRunnerField = _scenarioPartHelper.GetTestRunnerExpression(); @@ -208,7 +209,7 @@ private void SetupTestClassInitializeMethod(TestClassGenerationContext generatio _codeDomHelper.MarkCodeMethodInvokeExpressionAsAwait(onFeatureStartExpression); - testClassInitializeMethod.Statements.Add(onFeatureStartExpression); + testClassInitializeMethod.Statements.Add(onFeatureStartExpression);*/ } private void SetupTestClassCleanupMethod(TestClassGenerationContext generationContext) @@ -221,7 +222,7 @@ private void SetupTestClassCleanupMethod(TestClassGenerationContext generationCo _codeDomHelper.MarkCodeMemberMethodAsAsync(testClassCleanupMethod); _testGeneratorProvider.SetTestClassCleanupMethod(generationContext); - + /* var testRunnerField = _scenarioPartHelper.GetTestRunnerExpression(); // await testRunner.OnFeatureEndAsync(); @@ -244,7 +245,7 @@ private void SetupTestClassCleanupMethod(TestClassGenerationContext generationCo testClassCleanupMethod.Statements.Add( new CodeAssignStatement( testRunnerField, - new CodePrimitiveExpression(null))); + new CodePrimitiveExpression(null)));*/ } private void SetupTestInitializeMethod(TestClassGenerationContext generationContext) @@ -258,10 +259,6 @@ private void SetupTestInitializeMethod(TestClassGenerationContext generationCont _testGeneratorProvider.SetTestInitializeMethod(generationContext); - if (generationContext.UnitTestGeneratorProvider is not MsTestGeneratorProvider && - generationContext.UnitTestGeneratorProvider is not NUnit3TestGeneratorProvider) - return; // only MsTest & NUnit is implemented in this prototype - // Step 4: Obtain the test runner for executing a single test // testRunner = global::Reqnroll.TestRunnerManager.GetTestRunnerForAssembly(); @@ -373,10 +370,6 @@ private void SetupTestCleanupMethod(TestClassGenerationContext generationContext testCleanupMethod.Statements.Add(expression); - if (generationContext.UnitTestGeneratorProvider is not MsTestGeneratorProvider && - generationContext.UnitTestGeneratorProvider is not NUnit3TestGeneratorProvider) - return; // only MsTest & NUnit is implemented in this prototype - // Step 6: "Release" the TestRunner, so that other threads can pick it up (moved from TestClassCleanupMethod) // TestRunnerManager.ReleaseTestRunner(testRunner); testCleanupMethod.Statements.Add( diff --git a/Reqnroll.Generator/UnitTestProvider/XUnit2TestGeneratorProvider.cs b/Reqnroll.Generator/UnitTestProvider/XUnit2TestGeneratorProvider.cs index 7b15c51c8..6749f61d5 100644 --- a/Reqnroll.Generator/UnitTestProvider/XUnit2TestGeneratorProvider.cs +++ b/Reqnroll.Generator/UnitTestProvider/XUnit2TestGeneratorProvider.cs @@ -216,7 +216,8 @@ public void SetTestClassInitializeMethod(TestClassGenerationContext generationCo { // xUnit uses IUseFixture on the class generationContext.TestClassInitializeMethod.Attributes |= MemberAttributes.Static; - generationContext.TestRunnerField.Attributes |= MemberAttributes.Static; + // Step 1: make 'testRunner' instance field + // generationContext.TestRunnerField.Attributes |= MemberAttributes.Static; _currentFixtureDataTypeDeclaration = CodeDomHelper.CreateGeneratedTypeDeclaration("FixtureData"); generationContext.TestClass.Members.Add(_currentFixtureDataTypeDeclaration); @@ -245,6 +246,11 @@ public void SetTestClassInitializeMethod(TestClassGenerationContext generationCo CodeDomHelper.MarkCodeMethodInvokeExpressionAsAwait(expression); initializeMethod.Statements.Add(expression); + + + // Step 2: Remove TestClassInitializeMethod (not needed) + generationContext.TestClassInitializeMethod.Statements.Clear(); + //generationContext.TestClass.Members.Remove(generationContext.TestClassInitializeMethod); } public void SetTestClassCleanupMethod(TestClassGenerationContext generationContext) @@ -345,6 +351,10 @@ public void SetTestCleanupMethod(TestClassGenerationContext generationContext) CodeDomHelper.MarkCodeMethodInvokeExpressionAsAwait(expression); disposeMethod.Statements.Add(expression); + + // Step 3: Remove TestClassCleanupMethod (not needed) + generationContext.TestClassCleanupMethod.Statements.Clear(); + //generationContext.TestClass.Members.Remove(generationContext.TestClassCleanupMethod); } public void SetTestClassIgnore(TestClassGenerationContext generationContext)