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

Notification

Icon
Error

Print to Debug Windows in TestCaseSource Factory
CoolBreeze
#1 Posted : Thursday, September 1, 2016 4:05:02 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 7/11/2014(UTC)
Posts: 79
Location: United States of America

Was thanked: 9 time(s) in 9 post(s)
I want to print debug lines in my NUnit TestCaseSource Factory functions.

I've created several tests which insert new test records into my database. I created my own database transaction Rollback attribute and applied it to the test:

<Test()> _
<TestCaseSource("TestFactory")> _
<My.Rollback()> _
Public Sub Test(TestObj As Test)
--Do Test and Assert(s)
End Sub

Public Shared Function TestFactory() As Test

-- Insert some test records into database
-- Return List of Tests
End Function

What happens, of course, is that NCrunch calls TestFactory outside the Rollback. This means the new test records are not removed from the database
when the test finishes.

I found several of these TestFactorys where the test records are not rolled back. I moved the insert of the new test records into the test itself. Works great. New test records are inserted and when the test ends the Rollback AfterTest function is called, the transaction is rolled back, and the new test records are removed from the database.

However, I have several more TestFactorys which are inserting new test records and I can't find them (I have hundreds of tests).

So I thought I'll add Debug.WriteLine of the stack frames in the function that actually inserts the new test records.

That way NCrunch will call the TestFactory which in turn calls the DB function to insert the new test records, which calls Debug.WriteLine and I'll find
the TestFactorys from the stack frames.

I can't find where the Debug.WriteLines are writing to. I think I changed this to Console.WriteLine. I can't find the output.

OK. Maybe NCrunch is running in some special context when it calls TestFactorys and there's no real output destination to capture and display the Debug.WriteLines in that context.

I suppose I could write the stack frames to a file. But would be nicer to see output in Visual Studio.



Thanks, Ed
Remco
#2 Posted : Thursday, September 1, 2016 11:23:18 PM(UTC)
Rank: NCrunch Developer

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

Thanks: 957 times
Was thanked: 1287 time(s) in 1194 post(s)
Hi Ed,

The TestCaseSource factory methods are invoked by NUnit during test discovery, which happens inside the 'Analyse Assembly' tasks in the processing queue. NCrunch runs this after every successful build. The user code inside the factory methods is considered to be a part of the test discovery process. During the test discovery process, the tests themselves haven't been created yet and are unable to carry or represent data. For this reason, any trace data emitted by user code during the analysis task is not captured by NCrunch. However, if an exception is thrown inside a TestCaseSource factory method, this will interrupt the discovery process and bubble up through the task, where it will be displayed in the NCrunch tests window. So there is still at least one way you can get data out of it.

I generally recommend to try and keep TestCaseSource factory methods as simple as you possibly can. Because these methods are run in a context where the data collection is minimal and they cannot be parallelised, they have the potential to be a huge bottleneck and potential stability issue. Problems that appear within these methods can be hard to diagnose with NCrunch and can have far reaching consequences, affecting all tests in the suite.

A better approach would be to rig up each test so that it will clean up for any prior runs of other tests inside a SetUp for TestFixtureSetUp, then create its own data. In this way, no matter what your system is left in, the tests can always run consistently. Making tests clean up after themselves in a TearDown method always seems like an intuitive way to go, but there are situations where TestDown won't be called (for example, if your system loses power or the test process is unexpectedly terminated).
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.039 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download