0

I would like to have MSTest (honestly, I'd take whatever at this point) run my tests post-build, and then if any tests fail, produce the standard file(lineno): message output that Visual Studio recognizes and allows me to go straight to the failure point.

Or, if possible, then after a successful build I'd like the "Run All Tests in Solution" Visual Studio command to fire automatically.

As far as I can tell, this isn't possible. The commandline version of MSTest does not produce correctly formatted output (and as far as I can tell, neither does NUnit or xUnit), so when I add MSTest to my project's Post Build, I still have to scroll around, looking for the one that failed. The in-IDE version works ok, as long as I remember to hit Ctrl-R,A when the build finishes.

2
  • You might find some help at this post Commented May 29, 2011 at 6:52
  • Thanks, it has been years since I've needed a macro, I forgot VS could do that. I think that'll be my solution until the extension mentioned lower on the page is more mature. Commented May 29, 2011 at 12:05

2 Answers 2

1

You can get what you are looking for by making a custom target in your project file instead of using the pre-baked PostBuild target. Add something like this right into your project file, editing it as XML,

<Target Name="RunUnitTests"
  AfterTargets="CoreBuild">
  <Exec
    Command="mstest /testcontainer:$(OutDir)\PathToUnitTests\UnitTests.dll"
    CustomErrorRegularExpression="^Failed"
    />
</Target>

You'll need to properly set the path to your test assembly or whatever other method you are using (test config file etc.), and you may need to tweak the regex, going from memory here...

This will run mstest similar to how you are probably doing in the PostBuild, but adds in the ability for MSBuild (which is what drives the C# build system) to detect output strings that it should consider errors. There is a similar parameter for a CustomWarningRegularExpression also.

If you want to share this among multiple projects, look up "MSBuild Imports"

Sign up to request clarification or add additional context in comments.

2 Comments

That's very close. However, it lists the line in my .csproj as the error originating line. Trimmed down output: proj.csproj(140,5): error : [errorstacktrace] = at Unit_Tests. ... .SomethingTest() in ...\FileIActuallyCareAbout.cs:line 100
(That's with a custom regular expression of "^[errorstacktrace]".)
1

I've switched to xUnit, and with a minor change to their MSBuild unit test runner, I get what I want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.