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

Notification

Icon
Error

Is it possible to run each test in a separate process to prevent issues with mocked static methods?
aub3n
#1 Posted : Wednesday, May 22, 2013 3:00:47 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/22/2013(UTC)
Posts: 3
Location: Australia

Was thanked: 1 time(s) in 1 post(s)
Hi,

I'm experiencing some issues when mocking static methods using typemock and running the tests in ncrunch. When running the tests in a separate process everything works fine but when ncrunch runs the tests automatically they fail randomly. This seems to be caused by mocking the static method's return value (some tests expect fields on the returned object to have different values) and the order in which the tests are executed.

Is there any way that each test can be run in a separate process by default to avoid problems with mocked static methods?


Thanks.
dariusdamalakas
#2 Posted : Wednesday, May 22, 2013 6:09:53 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 1/10/2013(UTC)
Posts: 42
Location: Lithuania

Thanks: 4 times
Was thanked: 5 time(s) in 5 post(s)
I usually obstain from mocking static methods. However, if you are in control of the source code, you can use this example to modify static methods and allow them to be mocked:
http://blogs.clariuscons...ds-amenable-to-mocking/

I've used that in some cases in our code bases, and I find it more reliable than resorting to dark-magic (although this is debatable where typemock is dark magic, maybe grey one, or white with dots). Maybe that will help/give some ideas.
Remco
#3 Posted : Wednesday, May 22, 2013 6:50:56 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
Hi, thanks for posting!

NCrunch won't ever run tests concurrently within the same process, but it will re-use processes between execution runs. Traditional serial test runners don't do this, so not all tests have been engineered to consider it. Broadly, there are two ways to do what you're asking:

1. Adorn the tests involved with the IsolatedAttribute. Each test adorned with this attribute will be executed within a specially created process that is torn down as soon as the test has finished executing.

2. Set the Test process memory limit configuration setting to '1'. This will force NCrunch to terminate each test process after the process has finished executing a batch of tests.

Unfortunately, both of the above options will have a serious impact on NCrunch's performance - especially if you have a large number of small tests. I'm afraid I don't know enough about the behaviour of TypeMock around how it mocks static members to know for certain whether or not the random failures are being caused by a TypeMock/NCrunch clash, or something else. I recommend trying to narrow down the problem to make sure it isn't an issue caused by leftover state being re-used accidentally between tests. It only takes 1 erroneous test to corrupt the state of a test process and cause some very bizarre behaviour downstream in the test pipeline, and often these sorts of problems don't become visible until the project is run under NCrunch. You may find the troubleshooting guide helpful in further analysing this issue.

Cheers,

Remco
aub3n
#4 Posted : Thursday, May 23, 2013 9:57:13 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/22/2013(UTC)
Posts: 3
Location: Australia

Was thanked: 1 time(s) in 1 post(s)
Hi Remco,

thanks for your reply. I have added the Isolate attribute as well as changing the limit to 1 but it still randomly fails my tests. I have gotten in touch with TypeMock support to see if there is anything in my code that shouldn't be there.

Cheers
Remco
#5 Posted : Friday, May 24, 2013 3:22:25 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
Do you notice any difference if you set up NCrunch in compatibility mode? This would help to narrow down if the issue is being caused by NCrunch's environment, or something with TypeMock Isolator.
aub3n
#6 Posted : Monday, May 27, 2013 4:12:45 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/22/2013(UTC)
Posts: 3
Location: Australia

Was thanked: 1 time(s) in 1 post(s)
Hi Remco,

I'm seeing similar behavior in JustMock when mocking some static items so I'm not sure what the exact issue is right now. If I run the tests in NCrunch with Process Memory Limit set to 1 it works fine (for JustMock), if keeping the default setting (0) the tests fail. I'm in contact with their support as well to see what the issue is. The same issue also appears in ReSharper's test runner so I don't think it's NCrunch specific but if something turns up I'll make sure to post here.
1 user thanked aub3n for this useful post.
Remco on 5/27/2013(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.053 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download