testfx: Getting test output that breaks Visual Studio Devops Build pipeline (MSTest.Adapter v2.2.3)
Description
I was holding back on v2.2.1 and using v2.1.2 because of issue #789
When I upgraded to v2.2.2 and then to v2.2.3 I find the tests are running inside Visual Studio and the breaking behavior in 2.2.1 is resolved; however after running all the tests In the Test Output window I notice the following message
---------- Starting test run ----------
MSTest Executor: Test Parallelization enabled for C:\Path\To\My\Test\Project\bin\x64\Debug\net48\Tests.Integration.dll (Workers: 12, Scope: ClassLevel).
An exception occurred while invoking executor 'executor://mstestadapter/v2': The parameter cannot be null or empty.
Parameter name: message
========== Test run finished: 693 Tests run in 48.9 sec (693 Passed, 0 Failed, 0 Skipped) ==========
This doesn’t cause any issues in Visual studio but when they are executed in Visual Studio DevOps pipeline, the mention of the word exception
causes the Pipeline task to fail, even though all tests have passed.
I have rolled back to 2.1.2 multiple times, and after reverting, the above error message goes away.
Steps to reproduce
- Take test project with MSTest.Adapter version 2.1.2
- Upgrade to 2.2.3
Expected behavior
All tests should pass without any messages mentioning any exception
, e.g. when running same tests with v2.1.2
give the following message at end of run
---------- Starting test run ----------
MSTest Executor: Test Parallelization enabled for C:\Path\To\My\Test\Project\bin\x64\Debug\net48\Tests.Integration.dll (Workers: 12, Scope: ClassLevel).
========== Test run finished: 693 Tests run in 50.4 sec (693 Passed, 0 Failed, 0 Skipped) ==========
Actual behavior
I get the following message
---------- Starting test run ----------
MSTest Executor: Test Parallelization enabled for C:\Path\To\My\Test\Project\bin\x64\Debug\net48\Tests.Integration.dll (Workers: 12, Scope: ClassLevel).
An exception occurred while invoking executor 'executor://mstestadapter/v2': The parameter cannot be null or empty.
Parameter name: message
========== Test run finished: 693 Tests run in 48.9 sec (693 Passed, 0 Failed, 0 Skipped) ==========
I could be missing some Run parameters, but I couldn’t figure out which one exactly, quickly. Any pointers appreciated.
Environment
Visual Studio 2019 16.9.1 Windows 10 Enterprise x64 Test environment set to x64 explicitly Nuget Package Style: PackageReference
Package references in the project
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="EfCore.TestSupport" Version="3.2.0" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite" Version="3.1.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="3.1.13" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
<PackageReference Include="sqlite" Version="3.13.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 26 (17 by maintainers)
Commits related to this issue
- Fixes #799 by testing logged messages against "null or whitespace" instead of "null or empty" — committed to odalet/testfx by odalet-addup 3 years ago
- Fixes #799 by testing logged messages against "null or whitespace" instead of "null or empty" (#892) Co-authored-by: Olivier DALET <olivier.dalet@addupsolutions.com> — committed to microsoft/testfx by odalet 3 years ago
- Squashed commit of the following: commit f7de2d4fdeccbb318cdebeb23f0b488e5ac570b8 Author: dotnet-maestro[bot] <42748379+dotnet-maestro[bot]@users.noreply.github.com> Date: Wed Jul 21 14:26:59 2021 ... — committed to HeroMaxPower/testfx by HeroMaxPower 3 years ago
Well, to be honest, it was sheer luck that lead me to this workaround: I was merely trying to dump some information while investigating with my
Console.WriteLine
stuff… And then, I’m not that surprised the hack works with assembly level init/cleanup code as well: ultimately it taps into the sameLogCleanupResult
method deep down the test adapter.@Haplois I think the parts to investigate are what happens during initialization and cleanup steps relating to console redirections. Mongo2go may be specifically at fault (although it didn’t bother MSTest v2.1), but I suspect we’d end up with the same errors if we were to use any code that spawns a process and monitors its output.
I know from experience that Windows Console redirection is a b*tch to get right so I had a look at how this behaves on Linux: I ran my test test on WSL and… it just works!
Hi @odalet - thank you for all your work here. Since you did all the investigation, if you open the PR; I can guide you on the tests.
@Haplois I’ve just cloned the testfx repo, walked down the call stack of my exception, and I think I came up with a fix:
I noticed that in
TestExecutionManager.LogCleanupResult
, one makes sure that messages are neither null nor empty before passing them totestExecutionRecorder.SendMessage
.However (I took the liberty to examine the implementation of
ITestExecutionRecorder
from my Visual Studio installation), thisSendMessage
method throws if the message is null or whitespace! And I confirmed that, with my use case, I was indeed retrieving some output full of whitespaces and cr/lf…So I replaced the calls to
string.IsNullOrEmpty
withstring.IsNullOrWhitespace
(and added one such test inside the loop over warnings as well) and tried it against my test. And it works.Here is the proposed code for
TestExecutionManager.LogCleanupResult
:What’s left:
LogCleanupResult
or maybe the previous code didn’t capture at all the output from external processes…Tell me what you think!
@odalet, Thanks for all the investigation, hacking and narrowing down! I’ll look into this as well and try to see if we need fix this on our side.
@odalet that is a brilliant narrowing down. @Haplois I can confirm I am using Mongo2Go as well in my tests.
Haa haa… @odalet you wonderful hacker 😆 It works! Here is our Assembly level initialization
MockMongoFactory
is just a custom class where we initialize the DB with some custom params.using Microsoft.VisualStudio.TestTools.UnitTesting;
@Haplois Here is the exception I get if we don’t use the
Console.Out.WriteLine("...")
hack. Notice the first oneTests.Integration
passes and the second oneTests.Local
fails (because the second one doesn’t have the hack yet).Also I am using the release version
MSTest.TestAdapter
v2.2.4. So just confirming @odalet 's workaround works for previous builds too.I managed to narrow down the issue to a rather simple repro (note that you’ll need to add Mongo2go:
This test reproduces the error that prevent later tests to execute (in Visual Studio). What’s interesting is that:
Console.WriteLine
in the Cleanup method, the error goes away.MongoDbRunner.Start()
starts an external process (that runs mongo.exe) that in turn writes to its console. I suppose (did not check) that Mongo2go redirects mongo.exe out and err outputs.Well all this to say that I think there is some buggy behavior both in the Test initialization and cleanup methods with respect to console output maybe when some of this comes from out/err files redirected from an external process…
Hope this helps
@odalet thanks! If it’s a different issue you’re welcome to create a new issue for better visibility.