Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Takeover of PR #388 (Scope NUnit parallelization to generated classes instead of assembly-level) #432

Merged
merged 8 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
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,10 +6,9 @@
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]
[global::NUnit.Framework.FixtureLifeCycle(global::NUnit.Framework.LifeCycle.InstancePerTestCase)]
obligaron marked this conversation as resolved.
Show resolved Hide resolved
public static class PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks
{
[global::NUnit.Framework.OneTimeSetUp]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.Runtime.CompilerServices

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

<GeneratedCode("Reqnroll", "REQNROLL_VERSION")>
<FixtureLifeCycle(NUnit.Framework.LifeCycle.InstancePerTestCase)>
<SetUpFixture>
Public NotInheritable Class PROJECT_ROOT_NAMESPACE_NUnitAssemblyHooks
<OneTimeSetUp>
Expand Down
9 changes: 7 additions & 2 deletions Reqnroll.Generator/CodeDom/CodeDomHelper.cs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we replace the use of these custom functions with the more idiomatic form of
CodeTypeReference?

Example: new CodeTypeReference(typeof(Io.Cucumber.Messages.Types.Source), CodeTypeReferenceOptions.GlobalReference);

I've not verified that this works properly for VB generation but I would presume that it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. That's a good hint. I didn't know that that exists. We can try it in a new PR...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not verified that this works properly for VB generation but I would presume that it does.

Added a test to Messages for code gen using VB and this form of the CodeTypeReference api works as expected.
We should switch over to it as the custom function we have today for globalizing type names doesn't support VB.

I volunteer to draft the PR, but can wait until after this PR goes in.

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