Skip to content

Create a proper MSBuild ToolTask based VSTestTask #2702

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

Merged

Conversation

mcartoixa
Copy link
Contributor

@mcartoixa mcartoixa commented Jan 16, 2021

Description

Update from nohwnd:

A new task VSTestTask2 and a logger MSBuildLogger (for internal use only) was added. And can be enabled by setting /p:VsTestUseMSBuildOutput=true.

This is not enabled anywhere by default, neither in dotnet test, or under vstest, because I want to collect some feedback first.


The current console forwarding task has been renamed VSTestForwardingTask. The new VSTestTask, based on ToolTask, properly integrates into the MSBuild framework.
If the $(VSTestUseConsole) property is set to True during the build, the old console forwarding VSTestForwardTask is used: logs are lost but output is colorized (like today). Otherwise the new VSTestTask is used: logs are fine but colorization is lost (except for errors, that are properly recognized by MSBuild).

A new PR will be submitted right away on the dotnet/sdk project so that dotnet test sets the $(VSTestUseConsole) property.

In effect:

  • users that favor a colorized console output can use dotnet test (like today).
  • users that favor a proper MSBuild integration can use dotnet msbuild -Target:VSTest.

This PR feels more like a workaround than a proper fix: it still feels weird to depend on a MSBuild task (VSTestForwardingTask) that is not entirely MSBuild friendly... But it seems like the easiest fix that is compatible with the old behavior.
A proper fix would probably center around the implementation of dotnet test which could call dotnet exec vstest.console.dll directly (in addition to MSBuild, for the build part only). This would render current workarounds like for dotnet/msbuild#3130, or AllowFailureWithoutError definitely obsolete.

Related issue

Fixes (or at least gives an option around) #680, #1503, #2224, dotnet/sdk#9481, dotnet/msbuild#5387 and probably more...
This is a new attempt after #2437.

@mcartoixa
Copy link
Contributor Author

Please note that I would be happy to answer any questions and/or reservations, and/or discuss any suggestions or improvements on this PR.

@Haplois
Copy link
Contributor

Haplois commented Feb 6, 2021

Thank you for the contribution @mcartoixa, I'll check this during the week and ask my questions, if any.

@Haplois
Copy link
Contributor

Haplois commented Feb 6, 2021

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mcartoixa mcartoixa force-pushed the vstesttask-complete branch 2 times, most recently from 569e421 to cefe13d Compare May 26, 2021 06:40
@mcartoixa
Copy link
Contributor Author

mcartoixa commented May 26, 2021

I just rebased this PR against the main branch so that it would not be an excuse for not merging it 😉


activeProcess.WaitForExit();
Tracing.Trace("VSTest: Exit code: " + activeProcess.ExitCode);
return activeProcess.ExitCode == 0;
Copy link
Member

Choose a reason for hiding this comment

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

We should only return !Log.HasLoggedErrors. The task should fail if and only if it has logged any errors.

Copy link
Contributor Author

@mcartoixa mcartoixa Jun 30, 2021

Choose a reason for hiding this comment

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

I believe this is the way it's usually done in MSBuild tasks. Does vstest.console.exe return an error code even in cases of success?
Anyway this task is a copy (mostly left unchanged) of the current one, kept here for compatibility purposes. The goal of this PR is rather to introduce a new task that would be more respectful of MSBuild, see VSTestTask (the diff is rather useless on that one, though).


if (!string.IsNullOrEmpty(this.VSTestFramework))
{
Console.WriteLine(Resources.TestRunningSummary, this.TestFileFullPath, this.VSTestFramework);
Copy link
Member

Choose a reason for hiding this comment

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

A task shouldn't use Console.WriteLine, but instead use Log.LogMessage:
https://source.dot.net/#Microsoft.Build.Tasks.Core/Copy.cs,255

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed in general, but this has been left unchanged for compatibility with current behavior. The goal of this PR is rather to introduce a new task that would be more respectful of MSBuild, see VSTestTask.

// Avoid logging "Task returned false but did not log an error." on test failure, because we don't
// write MSBuild error. https://github.com/dotnet/msbuild/blob/51a1071f8871e0c93afbaf1b2ac2c9e59c7b6491/src/Framework/IBuildEngine7.cs#L12
var allowfailureWithoutError = BuildEngine.GetType().GetProperty("AllowFailureWithoutError");
allowfailureWithoutError?.SetValue(BuildEngine, true);
Copy link
Member

Choose a reason for hiding this comment

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

we should really not do this. If there was an error, log an MSBuild error. If there wasn't an error, don't log it.

Let's remove this reflection logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been left unchanged for compatibility with current behavior. The goal of this PR is rather to introduce a new task that would be more respectful of MSBuild, see VSTestTask.

@MarkKharitonov
Copy link

Just to make it clear to me. The VSTestUseConsole build property is introduced by this PR, which has not been merged yet, right?

@mcartoixa
Copy link
Contributor Author

@MarkKharitonov Right.
And to ensure compatibility would require the reopening of dotnet/sdk#15376 to initialize the VSTestUseConsole property in the dotnet test command.

@mcartoixa mcartoixa force-pushed the vstesttask-complete branch from cefe13d to 7acb932 Compare October 24, 2021 07:47
@mcartoixa
Copy link
Contributor Author

Just rebased again against main (like I seem to do every 5 months or so) just in case somebody suddenly were to be interested in getting this PR merged. 😉

@mcartoixa
Copy link
Contributor Author

When can we expect some kind of decision about the future of this PR? As mentioned before quite a few bugs could be affected by this.

  • Is it going to be rejected, and why?
  • Is it going to be merged, and when?

@mcartoixa
Copy link
Contributor Author

mcartoixa commented Apr 22, 2022

It took me 2 days to set up a new barely working development environment, but I think I managed to rebase this PR against main. Again.

@mcartoixa mcartoixa force-pushed the vstesttask-complete branch 2 times, most recently from d1e30d4 to 1b4b548 Compare April 22, 2022 20:00
@nohwnd nohwnd self-assigned this Apr 22, 2022
@mcartoixa
Copy link
Contributor Author

Any update on the status of this request?

@Nirmal4G
Copy link

nit: file should be renamed too!

test/Microsoft.TestPlatform.Build.UnitTests/VsTestTaskTests.cs -> VSTestTaskTests.cs

Copy link

@Nirmal4G Nirmal4G left a comment

Choose a reason for hiding this comment

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

Overall I like it. It's a welcome change but a huge breaking change nonetheless, so its up to the owners, I think!

@mcartoixa
Copy link
Contributor Author

@Nirmal4G Thanks for the review! There is nothing more that I would like than having a discussion with the owners.

Meanwhile it seems that I will have to rebase this PR again...

This was referenced Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants