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

Fix: NUnit projects fail or provide warning as TearDown : System.InvalidOperationException : Only static OneTimeSetUp and OneTimeTearDown are allowed for InstancePerTestCase mode. #323

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

## Bug fixes:

*Contributors of this release (in alphabetical order):*
* Fix: NUnit projects fail or provide warning as `TearDown : System.InvalidOperationException : Only static OneTimeSetUp and OneTimeTearDown are allowed for InstancePerTestCase mode.` (#320)

*Contributors of this release (in alphabetical order):* @gasparnagy

# v2.2.0 - 2024-11-07

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public virtual void SetTestClassInitializeMethod(TestClassGenerationContext gene

public virtual void SetTestClassCleanupMethod(TestClassGenerationContext generationContext)
{
generationContext.TestClassInitializeMethod.Attributes |= MemberAttributes.Static;
generationContext.TestClassCleanupMethod.Attributes |= MemberAttributes.Static;
CodeDomHelper.AddAttribute(generationContext.TestClassCleanupMethod, TESTFIXTURETEARDOWN_ATTR_NUNIT3);
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/Reqnroll.SystemTests/Generation/GenerationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public void GeneratorAllIn_sample_can_be_handled()
ExecuteTests();

ShouldAllScenariosPass();

ShouldFinishWithoutTestExecutionWarnings();
}

[TestMethod]
Expand Down
14 changes: 14 additions & 0 deletions Tests/Reqnroll.SystemTests/Generation/NUnitGenerationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ protected override void TestInitialize()
_testRunConfiguration.UnitTestProvider = UnitTestProvider.NUnit3;
}

[TestMethod]
public void GeneratorAllIn_sample_can_be_handled_by_NUnit4()
{
_testRunConfiguration.UnitTestProvider = UnitTestProvider.NUnit4;

PrepareGeneratorAllInSamples();

ExecuteTests();

ShouldAllScenariosPass();

ShouldFinishWithoutTestExecutionWarnings();
}

protected override void AssertIgnoredScenarioOutlineExampleHandled()
{
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
Expand Down
5 changes: 5 additions & 0 deletions Tests/Reqnroll.SystemTests/SystemTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ protected void ShouldAllScenariosPass(int? expectedNrOfTestsSpec = null)
_vsTestExecutionDriver.LastTestExecutionResult.Succeeded.Should().Be(expectedNrOfTests, "all tests should pass");
}

protected void ShouldFinishWithoutTestExecutionWarnings()
{
_vsTestExecutionDriver.LastTestExecutionResult.Warnings.Should().BeEmpty();
}

protected int ConfirmAllTestsRan(int? expectedNrOfTestsSpec)
{
if (expectedNrOfTestsSpec == null && _preparedTests == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public static string ToName(this UnitTestProvider unitTestProvider)
{
case UnitTestProvider.MSTest: return "MSTest";
case UnitTestProvider.NUnit2: return "NUnit2";
case UnitTestProvider.NUnit3: return "NUnit";
case UnitTestProvider.NUnit3:
case UnitTestProvider.NUnit4:
return "NUnit";
case UnitTestProvider.xUnit: return "XUnit";
default: throw new ArgumentOutOfRangeException(nameof(unitTestProvider), unitTestProvider, "value is not known");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class ProjectBuilder
public const string NUnit3PackageVersion = "3.13.1";
public const string NUnit3TestAdapterPackageName = "NUnit3TestAdapter";
public const string NUnit3TestAdapterPackageVersion = "3.17.0";
public const string NUnit4PackageName = "NUnit";
public const string NUnit4PackageVersion = "4.2.2";
public const string NUnit4TestAdapterPackageName = "NUnit3TestAdapter";
public const string NUnit4TestAdapterPackageVersion = "4.6.0";
private const string XUnitPackageVersion = "2.4.2";
private const string MSTestPackageVersion = "2.2.8";
private const string InternalJsonPackageName = "SpecFlow.Internal.Json";
Expand Down Expand Up @@ -253,7 +257,10 @@ private void EnsureProjectExists()
ConfigureXUnit();
break;
case UnitTestProvider.NUnit3:
ConfigureNUnit();
ConfigureNUnit3();
break;
case UnitTestProvider.NUnit4:
ConfigureNUnit4();
break;
default:
throw new InvalidOperationException(@"Invalid unit test provider.");
Expand All @@ -263,7 +270,7 @@ private void EnsureProjectExists()
AddAdditionalStuff();
}

private void ConfigureNUnit()
private void ConfigureNUnit3()
{
_project.AddNuGetPackage(NUnit3PackageName, NUnit3PackageVersion);
_project.AddNuGetPackage(NUnit3TestAdapterPackageName, NUnit3TestAdapterPackageVersion);
Expand All @@ -277,6 +284,20 @@ private void ConfigureNUnit()
}
}

private void ConfigureNUnit4()
{
_project.AddNuGetPackage(NUnit4PackageName, NUnit4PackageVersion);
_project.AddNuGetPackage(NUnit4TestAdapterPackageName, NUnit4TestAdapterPackageVersion);


if (IsReqnrollFeatureProject)
{
_project.AddNuGetPackage("Reqnroll.NUnit", _currentVersionDriver.ReqnrollNuGetVersion,
new NuGetPackageAssembly(GetReqnrollPublicAssemblyName("Reqnroll.NUnit.ReqnrollPlugin.dll"), "net462\\Reqnroll.NUnit.ReqnrollPlugin.dll"));
Configuration.Plugins.Add(new ReqnrollPlugin("Reqnroll.NUnit", ReqnrollPluginType.Runtime));
}
}

private void ConfigureXUnit()
{
if (_project.ProjectFormat == ProjectFormat.New)
Expand Down Expand Up @@ -348,6 +369,7 @@ private void AddUnitTestProviderSpecificConfig()
_project.AddFile(new ProjectFile("XUnitConfiguration.cs", "Compile", "using Xunit; [assembly: CollectionBehavior(CollectionBehavior.CollectionPerClass, MaxParallelThreads = 4)]"));
break;
case UnitTestProvider.NUnit3 when _parallelTestExecution:
case UnitTestProvider.NUnit4 when _parallelTestExecution:
_project.AddFile(new ProjectFile("NUnitConfiguration.cs", "Compile", "[assembly: NUnit.Framework.Parallelizable(NUnit.Framework.ParallelScope.All)]"));
break;
case UnitTestProvider.MSTest when _parallelTestExecution:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,29 @@ private TestExecutionResult CalculateTestExecutionResultFromTrx(XDocument trx, T
private TestExecutionResult GetCommonTestExecutionResult(XDocument trx, string output, IEnumerable<string> reportFiles, string logFileContent)
{
var testRunElement = trx.Descendants(_testRunElementName).Single();
var summaryElement = testRunElement.Element(_xmlns + "ResultSummary")?.Element(_xmlns + "Counters")
?? throw new InvalidOperationException("Invalid document; result summary counters element not found.");
var summaryElement = testRunElement.Element(_xmlns + "ResultSummary")
?? throw new InvalidOperationException("Invalid document; result summary element not found."); ;
var summaryCountersElement = summaryElement?.Element(_xmlns + "Counters")
?? throw new InvalidOperationException("Invalid document; result summary counters element not found.");

var totalAttribute = summaryElement.Attribute("total");
var executedAttribute = summaryElement.Attribute("executed");
var passedAttribute = summaryElement.Attribute("passed");
var failedAttribute = summaryElement.Attribute("failed");
var inconclusiveAttribute = summaryElement.Attribute("inconclusive");
var totalAttribute = summaryCountersElement.Attribute("total");
var executedAttribute = summaryCountersElement.Attribute("executed");
var passedAttribute = summaryCountersElement.Attribute("passed");
var failedAttribute = summaryCountersElement.Attribute("failed");
var inconclusiveAttribute = summaryCountersElement.Attribute("inconclusive");

int.TryParse(totalAttribute?.Value, out int total);
int.TryParse(executedAttribute?.Value, out int executed);
int.TryParse(passedAttribute?.Value, out int passed);
int.TryParse(failedAttribute?.Value, out int failed);
int.TryParse(inconclusiveAttribute?.Value, out int inconclusive);

var runInfos = summaryElement.Element(_xmlns + "RunInfos")?.Elements(_xmlns + "RunInfo") ?? Enumerable.Empty<XElement>();
var warnings = runInfos
.Where(ri => "Warning".Equals(ri.Attribute("outcome")?.Value, StringComparison.InvariantCultureIgnoreCase))
.Select(ri => ri.Element(_xmlns + "Text")?.Value ?? "[no warning text]")
.ToArray();

var testResults = GetTestResults(testRunElement, _xmlns);
var leafTestResults =
testResults.Where(tr => tr.InnerResults.Count == 0)
Expand All @@ -84,6 +92,7 @@ private TestExecutionResult GetCommonTestExecutionResult(XDocument trx, string o
Succeeded = passed,
Failed = failed,
Pending = inconclusive,
Warnings = warnings
};
}

Expand All @@ -93,7 +102,9 @@ private TestExecutionResult CalculateUnitTestProviderSpecificTestExecutionResult
{
case UnitTestProvider.xUnit: return CalculateXUnitTestExecutionResult(testExecutionResult, trx);
case UnitTestProvider.MSTest: return CalculateMsTestTestExecutionResult(testExecutionResult);
case UnitTestProvider.NUnit3: return CalculateNUnitTestExecutionResult(testExecutionResult);
case UnitTestProvider.NUnit3:
case UnitTestProvider.NUnit4:
return CalculateNUnitTestExecutionResult(testExecutionResult);
default: throw new NotSupportedException($"The specified unit test provider is not supported: {testRunConfiguration.UnitTestProvider}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class TestExecutionResult

public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }

public string[] Warnings { get; set; }
}

public class TestResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum UnitTestProvider
{
MSTest,
xUnit,
NUnit4,
NUnit3,
NUnit2
}