Build/Test Issues

System.Diagnostics.Contracts cause all related tests to fail

Started by RobertZ on 2,498 views

I am evaluating NCrunch to determine whether we want to use it and came across the following issue:

Tests executes fine in the test explorer however NCrunch fails with:
"NCrunch was unable to retrieve a meaningful result from this test due to an unexpected error - was the execution process terminated?"

Inserting this line into a method or constructor reproduces the problem:
Contract.Requires<InvalidOperationException>(false, "problem");

(System.Diagnostics.Contracts)

Ncrunch configuration is set to defaults.

"Ncrunch for visual studio 2019 - 4.6.0.3"

Any help is appreciated

Edited

Hi thanks for posting!

Are you perhaps using net5.0?

If so there's a know issue that was solved with the build that was released yesterday. Could you try using that one?

We also have another build that solves a few other issues related to the new logging system that was implemented. It should be released on the site soon. The download links for that build are:

NCrunch_Console_4.8.0.1.msi
NCrunch_Console_4.8.0.1.zip
NCrunch_GridNodeServer_4.8.0.1.msi
NCrunch_GridNodeServer_4.8.0.1.zip
NCrunch_LicenseServer_4.8.0.1.zip
NCrunch_VS2008_4.8.0.1.msi
NCrunch_VS2010_4.8.0.1.msi
NCrunch_VS2010_4.8.0.1.zip
NCrunch_VS2012_4.8.0.1.msi
NCrunch_VS2012_4.8.0.1.zip
NCrunch_VS2013_4.8.0.1.msi
NCrunch_VS2013_4.8.0.1.zip
NCrunch_VS2015_4.8.0.1.msi
NCrunch_VS2015_4.8.0.1.msi.7z
NCrunch_VS2015_4.8.0.1.zip
NCrunch_VS2017_4.8.0.1.msi
NCrunch_VS2017_4.8.0.1.msi.7z
NCrunch_VS2017_4.8.0.1.zip
NCrunch_VS2019_4.8.0.1.msi
NCrunch_VS2019_4.8.0.1.msi.7z
NCrunch_VS2019_4.8.0.1.zip
Hi,

I just tried to reproduce this and see the same issue even with the latest release. It looks like the test process is being terminated early. This needs a bit more investigation.
michaelkroes wrote:
Are you perhaps using net5.0?


.Net Core 3.1 at the moment however will likely be looking to upgrade to 5.0 soon.

I'll give the new version a go and see if this resolves and let you know.
Unfortunately still the same problem, going to have a go upgrading the solution to .net 5.0 and see if that resolves
michaelkroes wrote:Hi,

I just tried to reproduce this and see the same issue even with the latest release. It looks like the test process is being terminated early. This needs a bit more investigation.


Sorry posted before I saw this reply! Thanks for the confirmation!
We have a configuration setting for code contracts: https://www.ncrunch.net/documentation/reference_project-configuration_allow-static-code-contract-checks However, this doesn't work properly with netcore. We'll add a task to our backlog to investigate this further. In the mean time I'm looking for a workaround to this problem.

Code contracts aren't designed for background testing. The user dialog that pops up will terminate our test runner, preventing us from giving any feedback. (The same problem exists with the Visual Studio Test Explorer).
We've had an internal discussion on this after a deep look at what is happening here.

We cannot support code contracts under .NET Core. The system wasn't designed for background testing and will always terminate the test process when a condition fails. There are no hooks in this process for us without performing memory hacks on the platform, which would unacceptably risk destabilising things.

Under .NET Framework, there were build options available to deactivate code contracts, which is how we implemented the NCrunch config options to turn it off. Unfortunately, under .NET Core these options no longer appear to exist in any useful state, with the system itself baked into the platform.

This means the only way we could get NCrunch to work at all on a project using code contracts would be to scan every built user assembly at IL-level and forcefully remove the references to code contracts types in the system. This scanning would be expensive and would decrease the engine's performance for everyone, regardless of whether they used code contracts or not. Given that we care very much about the performance of the engine, this really isn't a workable solution for us.

Thus the only option here is for you to not use code contracts with NCrunch. If you need to use this feature, place it behind a facade that you control and put the contracts calls inside '#if !NCRUNCH' blocks to make sure they never get executed under NCrunch.

Post a reply

Log in to reply.