Skip to content

Commit

Permalink
Takeover of PR #388 (Scope NUnit parallelization to generated classes…
Browse files Browse the repository at this point in the history
… instead of assembly-level) (#432)

* Replace assembly-wide hook for C#

* Replace assembly-wide hook for VB

* change for CS

* change for VB

* add FixtureLifeCycleAttribute to generetd classes

* Add CHANGELOG entry

* Remove FixtureLifeCycle attribute from SetupFixture classes

---------

Co-authored-by: Sean Killeen <[email protected]>
  • Loading branch information
gasparnagy and SeanKilleen authored Feb 7, 2025
1 parent 182e933 commit 60cab92
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

* Fix: Replace deprecated dependency `Specflow.Internal.Json` with `System.Text.Json`. The dependency was used for laoding `reqnroll.json`, for Visual Studio integration and for telemetry. (#373)
* Fix: Support loading plugin dependencies from .deps.json on .NET Framework and Visual Studio MSBuild (#408)
* Fix: Error with NUnit 4: "Only static OneTimeSetUp and OneTimeTearDown are allowed for InstancePerTestCase mode" (#379)

*Contributors of this release (in alphabetical order):* @clrudolphi, @obligaron, @olegKoshmeliuk
*Contributors of this release (in alphabetical order):* @clrudolphi, @gasparnagy, @obligaron, @olegKoshmeliuk, @SeanKilleen

# v2.2.1 - 2024-11-08

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using global::System.Runtime.CompilerServices;
using System.Threading.Tasks;

[assembly: NUnit.Framework.FixtureLifeCycle(NUnit.Framework.LifeCycle.InstancePerTestCase)]

[GeneratedCode("Reqnroll", "REQNROLL_VERSION")]
[global::NUnit.Framework.SetUpFixture]
public static class PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.Runtime.CompilerServices

<assembly: NUnit.Framework.FixtureLifeCycle(NUnit.Framework.LifeCycle.InstancePerTestCase)>

<GeneratedCode("Reqnroll", "REQNROLL_VERSION")>
<SetUpFixture>
Public NotInheritable Class PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks
Expand Down
9 changes: 7 additions & 2 deletions Reqnroll.Generator/CodeDom/CodeDomHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,18 @@ private CodeExpression GetAwaitedMethodThisTargetObject(CodeExpression thisExpre
}

public string GetGlobalizedTypeName(Type type)
{
return GetGlobalizedTypeName(type.FullName!);
}

public string GetGlobalizedTypeName(string typeName)
{
if (TargetLanguage == CodeDomProviderLanguage.CSharp)
{
return "global::" + type.FullName!;
return "global::" + typeName;
}
// Global namespaces not yet supported in VB
return type.FullName!;
return typeName;
}

public CodeExpression CreateOptionalArgumentExpression(string parameterName, CodeVariableReferenceExpression valueExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class NUnit3TestGeneratorProvider : IUnitTestGeneratorProvider
protected internal const string TESTFIXTURETEARDOWN_ATTR_NUNIT3 = "NUnit.Framework.OneTimeTearDownAttribute";
protected internal const string NONPARALLELIZABLE_ATTR = "NUnit.Framework.NonParallelizableAttribute";
protected internal const string TESTFIXTURE_ATTR = "NUnit.Framework.TestFixtureAttribute";
protected internal const string FIXTURELIFECYCLE_ATTR = "NUnit.Framework.FixtureLifeCycleAttribute";
protected internal const string LIFECYCLE_CLASS = "NUnit.Framework.LifeCycle";
protected internal const string LIFECYCLE_INSTANCEPERTESTCASE = "InstancePerTestCase";
protected internal const string TEST_ATTR = "NUnit.Framework.TestAttribute";
protected internal const string ROW_ATTR = "NUnit.Framework.TestCaseAttribute";
protected internal const string CATEGORY_ATTR = "NUnit.Framework.CategoryAttribute";
Expand Down Expand Up @@ -70,6 +73,9 @@ public void SetTestClass(TestClassGenerationContext generationContext, string fe
{
CodeDomHelper.AddAttribute(generationContext.TestClass, TESTFIXTURE_ATTR);
CodeDomHelper.AddAttribute(generationContext.TestClass, DESCRIPTION_ATTR, featureTitle);
CodeDomHelper.AddAttribute(generationContext.TestClass, FIXTURELIFECYCLE_ATTR,
new CodeAttributeArgument(
new CodeSnippetExpression($"{CodeDomHelper.GetGlobalizedTypeName(LIFECYCLE_CLASS)}.{LIFECYCLE_INSTANCEPERTESTCASE}")));
}

public void SetTestClassCategories(TestClassGenerationContext generationContext, IEnumerable<string> featureCategories)
Expand Down

0 comments on commit 60cab92

Please sign in to comment.