0

I am writing new tests in Nunit. I would like the tests to get their TestCaseSource values from an excel sheet (Data-driven tests).

However, I noticed that the [SetUp] method is actually accessed AFTER the [Test] method is entered, therefore I cannot initialize the data I read from my excel sheet in the TestCaseSource.

How do I init my TestCaseSource from an excel file BEFORE each test is running?

Thanks

I have tried using a separate class like MyFactoryClass and then used

[Test, TestCaseSource(typeof(MyFactoryClass), "TestCases")]

However, this is reached Before the [Setup] method and does not recognize the name of the excel file that is named after each tests' name.

Chandresh Khambhayata
  • 1,748
  • 2
  • 31
  • 60

1 Answers1

0

It's important, when using NUnit, to understand the stages that a test goes through as it is loaded and then run. Because I don't know what you are doing at each stage, I'll start by outlining those stages. I'll add to this answer after you post some code that shows what your factory class, your [SetUp] method and your actual tests are doing.

In brief, NUnit loads tests before it runs them. It may actually run tests multiple tiems for each load - this depends on the type of runner being used. Examples:

  1. NUnit-console loads tests once and runs them once, then exits.

  2. TestCentric GUI loads tests once and then runs them each time you select tests and click run. It can reload them using a menu option as well.

  3. TestExplorer, using the NUnit 3 Test Adapter, loads tests and then runs them each time you click run.

Ideally, you should write your tests so that they will work under any runner. To do that, you should assume that they will be run multiple times for each load. Don't write code at load time, which you want to see repeated for each run. If you follow this rule, you'll have more robust tests.

So... what does NUnit do at each stage? Here it is...

Loading...

  • All the code in your [TestCaseSource] executes.

Running...

  • For each TestFixture (I'll ignore SetUpFixtures for simplicity)

    • Run any [OneTimeSetUp] method

    • For each Test or TestCase

      • Run any [SetUp] method
      • Run the test itself
      • Run any [TearDown] method
    • Run any [OneTimeTearDown] method

As you noticed, the code you write for any step can only depend on steps that have already executed. In particular, the action taken when loading the test can't depend on actions that are part of running it. This makes sense if you consider that "loading" really means creating the test that will be run.

In your [TestCaseSource] you should only call a factory that creates objects if you know in advance what objects to create. Usually, the best approach is to initialize those parameters that will be used to create objects. Those are then used to actually create the objects in the [OneTimeSetUp] or [SetUp] depending on the object lifetime you are aiming for.

That's enough (maybe too much) generalization! If you post some code, I'll add more specific suggestions to this answer.

Charlie
  • 12,928
  • 1
  • 27
  • 31
  • I don't have much code actually. I am trying to figure out how to do data driven test, and to find out if it was possible to read my test parameters from an excel sheet. I thought I could read my parameters from an outside source and then pass the parameters somehow to the [TestCaseSource] – user12272269 Oct 30 '19 at 00:04