Hi,
I was updating a test and expected New, Setup, Factory, and Test methods to be called in a specific order, but they were not.
To understand the call order of methods in a test calls:
I wrote a simple test class to write out the name of the method it was called in (I pasted the code below).
I expected to see methods called in this order:
1) New
2) OneTimeSetup
3) Test_Factory
4) Setup [or maybe 3) Setup, then 4) Test_Factory.]
5) Test1("George")
6) TearDown
7) Setup
8) Test1("George")
9) TearDown
10) OneTimeTearDown
But, it appears methods are not called in that order:
If I select the test class in the NCrunch Tests window and run in Debug mode I see:
1) Test_Factory
2) New
3) OneTimeSetup
4) Setup
5) Test1("George")
6) TearDown
7) Setup
8) Test1("Washington")
9) TearDown
10) OneTimeTearDown
What happens is:
Test_Factory runs and expects an object to be initialized by New or OneTimeSetup. But, since it's not initialized it's Nothing\Null.
In this case the S string is not initialized by New(). So, Test_Factory adds S which is Nothing to the L list.
NCrunch passes Nothing to Test1.
When the test method actually attempts to do S.Trim() I get the expected:
System.NullReferenceException : Object reference not set to an instance of an object.
But, if New or OneTimeSetup ran first it would have set S = "The brown fox jumped over the moon."
Then the exception would not have occurred in Test1.
Maybe I'm overlooking something about what the call order is of the methods.
The other behavior I see is:
Also, I noticed if I Run the tests from the class level I see in the Trace Output window:
Sub New
OneTimeSetup - Called once to perform any setup before any child tests are run.
OneTimeTearDown - Called once after all the child tests have run.
I notice there is no output from the Debug.WriteLine from New, Test_Factory, Test, Setup, and TearDown.
If I run an individual test I see in the Trace Output window:
Setup - Called before each test run.
Test1: George
TearDown - Called immediately after every test is run
I notice there is no output from the Debug.WriteLines from New, Test_Factory, OneTimeSetup, and OneTimeTearDown.
I set breakpoints on each Debug.WriteLine in each method, ran in Debug mode the tests either at the class level or individual level.
Then I wrote down the name of the method.
My test class:
Imports NUnit.Framework
<TestFixture()>
Public Class NUnit_Life_Cycle
Private Shared S As String = Nothing
Public Sub New()
S = "The brown fox jumped over the moon."
Debug.WriteLine("Sub New")
End Sub
<OneTimeSetUp()>
Public Sub OneTimeSetup()
S = "The brown fox jumped over the moon."
Debug.WriteLine("OneTimeSetup - Called once to perform any setup before any child tests are run.")
End Sub
<OneTimeTearDown()>
Public Sub OneTimeTearDown()
Debug.WriteLine("OneTimeTearDown - Called once after all the child tests have run.")
End Sub
<SetUp>
Public Sub Setup()
Debug.WriteLine("Setup - Called before each test run.")
End Sub
<TearDown()>
Public Sub TearDown()
Debug.WriteLine("TearDown - Called immediately after every test is run.")
End Sub
<Test()>
<TestCaseSource("Test_Factory")>
Public Sub Test1(S As String)
Dim Message As String
S.Trim()
Message = String.Format("Test1: {0}", S)
Debug.WriteLine(Message)
End Sub
Public Shared Function Test_Factory() As List(Of String)
Dim L As New List(Of String)
Debug.WriteLine("Test_Factory")
L.Add("George")
L.Add("Washington")
L.Add(S)
Return L
End Function
End Class