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

Notification

Icon
Error

Debug.Assert messages should be filtered out
latin_programmer
#1 Posted : Friday, November 30, 2012 8:44:21 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/29/2012(UTC)
Posts: 3

Was thanked: 2 time(s) in 1 post(s)
I think this is a known issue of NCrunch:
https://twitter.com/ONE7...uses/159648603021918209

I came across it this week though when we added some unit tests that were intended to test failure paths. Now each time that the tests are run I keep getting the Debug.Assert windows that you would get when running the application itself.

I would suggest to either filter out Debug.Assert messages or give us an option to do so under preferences.

I'm using NCrunch for Visual Studio 2012 version 1.42.0.12

To reproduce: Create a method with a Debug.Assert message. Create a unit test that fails said Debug.Assert message. Now a popup window with the Debug.Assert warning will show up each time NCrunch re-runs the tests.
Remco
#2 Posted : Friday, November 30, 2012 11:04:27 PM(UTC)
Rank: NCrunch Developer

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

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi, thanks for sharing this issue!

I can see how this would be frustrating. Would you be able to share some more details about how your code normally works outside NCrunch? My expectation is that other test runners would likely behave the same (although I'm not suggesting that this would be a model approach). Do you use conditional compilation of some sort to filter out the Debug Asserts while performing testing outside NCrunch?


Cheers,

Remco
GreenMoose
#3 Posted : Monday, December 3, 2012 10:25:07 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 503

Thanks: 142 times
Was thanked: 66 time(s) in 64 post(s)
You may have luck configuring the UI behavior via system.diagnostics configuration in e.g. a config file (see http://msdn.microsoft.co...s/library/ty5e4c4h.aspx ).

However, what you as developer need to consider is what you expect to happen when an assertion fails. Is this part of the code you are testing so the test should pass or do you want a test to fail if an assertion fails.
One way to handle this is to globally replace an assertion failure with an exception for tests (still use assertions in code but intercept them them e.g. via a trace listener), and in those scenarios you want to "ignore" assertion failures you use a custom listener, or customize behavior of current, to not throw an exception during an assertion failure.

*Edit: Regarding the UI dialogs themselves, I don't think this is NCrunch specific. I have experienced the same behavior with other test runners (e.g. ReSharper).
latin_programmer
#5 Posted : Monday, December 3, 2012 11:05:09 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/29/2012(UTC)
Posts: 3

Was thanked: 2 time(s) in 1 post(s)
Thank you for your replies Remco and GreenMoose.

First I'm going to try to answer Remco's question about how the code works outside NCrunch. And perhaps use that to comment further on GreenMoose's suggestion.

I am the only one using NCrunch in my organization right now. I paid for a license so that I could use the product for a while and determine whether we should buy licenses for the other developers in my team. Currently, we have actually very few uses of Debug.Assert in our code. I of course cannot disclose our codebase due to copyright and privacy concerns, but I think it will be OK to share the code that is causing trouble for me right now:

Quote:
Debug.Assert(inspection.UserID.GetValueOrDefault() > 0, "User ID should always be set on the inspection before trying to add to database.");


Now, someone outside my team decided to add a test case in which UserID is indeed not set and then verify that the application fails in a certain way. If I run the unit tests with ReSharper I do get a popup alert for the Debug.Assert and I have to choose whether to abort/retry/ignore. If I choose "ignore" the test is successful. However, I don't have ReSharper setup to run my unit tests continuously like I do for NCrunch. Right now, I just saved a JavaScript file in my solution and I got two new popup windows for the unit tests that were re-run. I'm having to constantly do this to work in my solution, which is indeed something that I must address. I'm faced then with some choices:
  • Remove the Debug.Assert statements.
  • Redirect the output Debug.Assert statements as indicated by GreenMoose
  • Remove the unit tests from my run.


None of those choices are ideal. I cannot ask developers in my team to stop using Debug.Assert so that NCruch works on my machine without showing the alerts. Redirecting the input of the Debug.Assert statements might be a better workaround, but it reduces the benefits of the Debug.Assert statements in the first place (and again, this is a workaround that I would need to ask other developers to use to address a problem of a tool that is only used by me). Removing the unit test from my list of tests is of course the worst option since I lose the benefit from the unit test altogether.

Considering the scenario above, I would suggest to have NCrunch give us the option to filter out the Debug.Assert statements.

Thank you again for your responses,

- LP
Remco
#6 Posted : Tuesday, December 4, 2012 3:50:41 AM(UTC)
Rank: NCrunch Developer

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

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi LP -

Thanks for the detailed explanation. This really helps a lot with understanding the reasoning behind why your codebase works the way it does, and the constraints of the environment you're working in. I'm not sure if I can implement a direct solution in NCrunch that is specific to this problem (as I feel your situation is very specialised), but I will try to explain my reasoning behind this and hopefully I can inspire some more thought about other approaches to solving this problem.

On a technical level, this problem can be solved in NCrunch by mutating the input source code during processing by the NCrunch engine, or by manipulating the assembly output in NCrunch's build process (in a similar way as NCrunch adds instrumentation for profiling code coverage and performance). There are, however, some drawbacks to this:

- The behaviour would need to be conditional on a configuration setting, as I'm not entirely convinced that all users would want it. This would mean yet another configuration setting to add to the existing forest of configuration settings, making the tool harder to learn and understand
- There would be a performance penalty for needing to inspect the code to make the alterations to the Debug.Assert statements, which would become noticeable for large projects
- It further moves the code running within NCrunch's environment out of alignment with normal code behaviour, which can create unexpected situations that may be difficult for people to analyse (i.e. its 'magic')

As an alternative, there are few things I can suggest that may either mitigate or completely solve this problem for you:

- Set the Use build configuration setting for the project containing Debug.Assert statements to 'RELEASE' (or your own equivalent of a release build configuration name). This will force NCrunch to build the project in release mode, thus neglecting to compile the Debug.Assert statements. NCrunch will automatically introduce overrides into your build process to allow it to continue to analyse code coverage and performance data.

- Add #IF !NCRUNCH compiler conditional statements around the Debug.Assert calls. This will force NCrunch to ignore them at build time. Hopefully your team members won't delete these if they find them. A tidy way could be to wrap the call to Debug.Assert with something else, then place the compiler condition in the wrapper method. If you don't have many of these calls around and they are introduced infrequently, this may be the easiest solution.

- Speak with your team members about the usage of Debug.Assert in regards to automated testing. The problems you experience with this statement may go well beyond just NCrunch, as it effectively makes it impossible to have unattended test runs (i.e. running your tests over lunch, or on a build server). You may find that an accumulated number of these statements quickly brings any form of automated testing to a standstill, forcing you to forever sit around and click 'Ignore' again and again.

I hope this helps!


Cheers,

Remco
GreenMoose
#4 Posted : Tuesday, December 4, 2012 7:45:13 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 503

Thanks: 142 times
Was thanked: 66 time(s) in 64 post(s)
GreenMoose;3320 wrote:
You may have luck configuring the UI behavior via system.diagnostics configuration in e.g. a config file (see http://msdn.microsoft.co...s/library/ty5e4c4h.aspx )...


Before trying everything else non-trivial stuff (except for running tests in e.g. Release with NCrunch), ensure you try disabling the assert UI via the built-in net method (e.g. in config file) as stated above. After all, .net framework already contains logic to suppress assert dialogs from popping up.

p.s. In our team we simply use our custom debug class with assertion method, which, based on the context, calls .net's assertion, throws an exception, or ignores it completely (e.g. explicitly suppressed in a test) d.s.
latin_programmer
#7 Posted : Tuesday, December 4, 2012 9:08:26 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/29/2012(UTC)
Posts: 3

Was thanked: 2 time(s) in 1 post(s)
Changing the NCrunch configuration to Release is an acceptable solution for me. This is something I can do in my machine without affecting anybody else. I changed my configuration in my local and the Debug.Assert statements are no longer showing up so this issue has now been resolved on my end.

GreenMoose;3330 wrote:
Before trying everything else non-trivial stuff (except for running tests in e.g. Release with NCrunch), ensure you try disabling the assert UI via the built-in net method (e.g. in config file) as stated above. After all, .net framework already contains logic to suppress assert dialogs from popping up.

p.s. In our team we simply use our custom debug class with assertion method, which, based on the context, calls .net's assertion, throws an exception, or ignores it completely (e.g. explicitly suppressed in a test) d.s.


I addressed your suggestion briefly in my previous post. This was the next best solution. It was still not ideal in my case because there might be reasons why you want to have the .NET assertion popup when running a unit test manually (which I understand is also why NCrunch doesn't disable this in the first place). In any case, I was not trying to make an argument of whether popup assertions are good or needed but rather whether the user should be given the choice of suppressing them for continuous tests in NCrunch while still keeping them for everything else.

Remco;3329 wrote:
The problems you experience with this statement may go well beyond just NCrunch, as it effectively makes it impossible to have unattended test runs (i.e. running your tests over lunch, or on a build server). You may find that an accumulated number of these statements quickly brings any form of automated testing to a standstill, forcing you to forever sit around and click 'Ignore' again and again.


We do have unattended test runs in TeamCity and they were not affected by the Debug.Assert statements.

Thank you again for your replies,

-LP
2 users thanked latin_programmer for this useful post.
Remco on 12/5/2012(UTC), openshac on 1/27/2016(UTC)
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.168 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download