Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

System.Diagnostics.Contracts cause all related tests to fail
RobertZ
#1 Posted : Wednesday, February 17, 2021 3:19:32 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/17/2021(UTC)
Posts: 4
Location: United Kingdom

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
michaelkroes
#2 Posted : Wednesday, February 17, 2021 3:24:02 PM(UTC)
Rank: NCrunch Developer

Groups: Registered
Joined: 9/22/2017(UTC)
Posts: 240
Location: Netherlands

Thanks: 104 times
Was thanked: 52 time(s) in 50 post(s)
michaelkroes
#4 Posted : Wednesday, February 17, 2021 3:33:01 PM(UTC)
Rank: NCrunch Developer

Groups: Registered
Joined: 9/22/2017(UTC)
Posts: 240
Location: Netherlands

Thanks: 104 times
Was thanked: 52 time(s) in 50 post(s)
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.
RobertZ
#3 Posted : Wednesday, February 17, 2021 3:51:11 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/17/2021(UTC)
Posts: 4
Location: United Kingdom

michaelkroes;15328 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.
RobertZ
#6 Posted : Wednesday, February 17, 2021 4:04:39 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/17/2021(UTC)
Posts: 4
Location: United Kingdom

Unfortunately still the same problem, going to have a go upgrading the solution to .net 5.0 and see if that resolves
RobertZ
#5 Posted : Wednesday, February 17, 2021 4:12:12 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/17/2021(UTC)
Posts: 4
Location: United Kingdom

michaelkroes;15329 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!
michaelkroes
#7 Posted : Thursday, February 18, 2021 10:36:15 AM(UTC)
Rank: NCrunch Developer

Groups: Registered
Joined: 9/22/2017(UTC)
Posts: 240
Location: Netherlands

Thanks: 104 times
Was thanked: 52 time(s) in 50 post(s)
We have a configuration setting for code contracts: https://www.ncrunch.net/...ic-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).
Remco
#8 Posted : Thursday, February 18, 2021 11:06:21 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,319

Thanks: 819 times
Was thanked: 1096 time(s) in 1039 post(s)
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.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.052 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download