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

Avoid more overhead of assembly initialize #4349

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ public void RunClassInitialize(TestContext testContext)

internal UnitTestResult GetResultOrRunClassInitialize(ITestContext testContext, string initializationLogs, string initializationErrorLogs, string initializationTrace, string initializationTestContextMessages)
{
if (IsClassInitializeExecuted)
{
return new UnitTestResult(ObjectModelUnitTestOutcome.Passed, null);
}

bool isSTATestClass = AttributeComparer.IsDerived<STATestClassAttribute>(ClassAttribute);
bool isWindowsOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isSTATestClass
Expand Down
16 changes: 13 additions & 3 deletions src/Adapter/MSTest.TestAdapter/Execution/UnitTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,25 @@ internal UnitTestResult[] RunSingleTest(TestMethod testMethod, IDictionary<strin
_fixtureTests.TryAdd(testMethod.AssemblyName, testMethodInfo);
_fixtureTests.TryAdd(testMethod.AssemblyName + testMethod.FullClassName, testMethodInfo);

UnitTestResult assemblyInitializeResult = RunAssemblyInitializeIfNeeded(testMethodInfo, testContext);
UnitTestResult? assemblyInitializeResult = null;

if (assemblyInitializeResult.Outcome != UnitTestOutcome.Passed)
if (!testMethodInfo.Parent.Parent.IsAssemblyInitializeExecuted)
{
assemblyInitializeResult = RunAssemblyInitializeIfNeeded(testMethodInfo, testContext);
}

if (assemblyInitializeResult != null && assemblyInitializeResult.Outcome != UnitTestOutcome.Passed)
Comment on lines +165 to +170
Copy link
Member

Choose a reason for hiding this comment

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

This changes the semantics of the code I think.

Previously, if assembly initialize failed, we will attach the failure for every single test run. While now, it's only attached for the first test.

Copy link
Member Author

Choose a reason for hiding this comment

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

from the code it looks like we prepare to capture outputs of the initialize method in every single test, but we actually run zero times or one time. so hopefully this changes nothing. We just prevent setting up capturing of output for method that will immediately return.

{
result = [assemblyInitializeResult];
}
else
{
UnitTestResult classInitializeResult = testMethodInfo.Parent.GetResultOrRunClassInitialize(testContext, assemblyInitializeResult.StandardOut!, assemblyInitializeResult.StandardError!, assemblyInitializeResult.DebugTrace!, assemblyInitializeResult.TestContextMessages!);
UnitTestResult classInitializeResult = testMethodInfo.Parent.GetResultOrRunClassInitialize(
testContext,
assemblyInitializeResult?.StandardOut ?? string.Empty,
assemblyInitializeResult?.StandardError ?? string.Empty,
assemblyInitializeResult?.DebugTrace ?? string.Empty,
assemblyInitializeResult?.TestContextMessages ?? string.Empty);
DebugEx.Assert(testMethodInfo.Parent.IsClassInitializeExecuted, "IsClassInitializeExecuted should be true after attempting to run it.");
if (classInitializeResult.Outcome != UnitTestOutcome.Passed)
{
Expand Down
Loading