Neeraj
Neeraj

Reputation: 4489

Why [OneTimeSetUp] in [SetUpFixture] called after (Test,TestCaseSource())

I am stuck in strange situation in Nunit 3 testcases, My [OneTimeSetUp] in [SetUpFixture] is called after [Test, TestCaseSource(typeof(TestDataProvider), "GetSQLTestCases")].

Where as when I remove that TestCaseSource attribute from tests, then work as expected in order as given below.

OneTimeSetup -- > Test

Please advice me how to resolve said issue.

-Neeraj

Upvotes: 4

Views: 1117

Answers (1)

user2864740
user2864740

Reputation: 61885

That is expected. All TestCaseSource targets are evaluated BEFORE any tests (or any SetUp/OneTimeSetUp method) is called.

The order is:

(~ALL~ TestCaseSource in assembly) ->
  (SetupFixture ctor) ->
    (OneTimeSetUp) ->
      (TestFixture ctor) ->
        (SetUp) ->
          (Test)

This is because the RESULT of the TestCaseSource's is the list of data used to execute the tests; as this is the list of tests that will be run, it is generated at the start. (Old NUnit versions would create instances of classes to access non-static TestCaseSource targets.)

While likely inadvisable1, the TestCaseSource's backing static method could "load database stuff"2, as long as it doesn't depend on OneTimeSetUp being called first. TestCaseSources should be FAST3 and resource-free as the time taken is "phantom time"4 not accounted for within test.

1,2,3,4Failures to evaluate TestCaseSources, such as database errors, will result in tests being not-discoverable. All time taken evaluating TestCaseSources will slow down every test exploration. The time overhead of this evaluation is not included in individual test timings (even when running single tests) and is not shown using standard tooling. It is tedious to track down and fix such slow TestCaseSources later.

Upvotes: 3

Related Questions