Search code examples
nunitmstestnunit-3.0nunit-console

nUnit isnt executing OneTimeSetup first but executing OneTimeTearDown


I recently made the switch from MSTest to nUnit to run my selenium test. With MSTest i was running my test as assembly initialized. I read that i could do something similar and removed and added all the necessary elements to run my test using NUnit but the order of execution from what i read is not working as i expected or i may not be able to do this.

Version of Nunit = 3.12.0

NUnit3TestAdapter = 3.17.0

Below is example of my code with cs file having my test cases and another cs file having my setup fixture like below all are in the same namespace.

The issue i'm having is when I run my test case the attribute OneTimeTearDown is getting executed first before anything else instead of the OneTimeSetup.

      using System;
  using NUnit.Framework;
namespace Automation_Test
{

  [TestFixture]
  public class Custom_Tests
  {
    [Test]
    public void Add()
    { /* ... */ }
  }
}

The file that used to contain my assembly initialized

using System;
  using NUnit.Framework;
namespace Automation_Test
{

   [SetUpFixture]
  public class AssemblyInitializer
  {
    [OneTimeSetUp]
    public static void LoginSystem()
    { /* ... */ }

    [OneTimeTearDown] <---- This method is getting trigger first when i run the test instead of onetimesetup
    public static void AssemblyCleanup()
    { /* ... */ }

  
  }
}

Solution

  • The issue I had was placing the Playback settings in OneTimeSetup method

     Playback.PlaybackSettings.DelayBetweenActions = 750;
     Playback.PlaybackSettings.ThinkTimeMultiplier = 2;
    

    Which i didn't show in question. This was causing a silent exception which I didn't see occur which would go and bypass

       [OneTimeSetUp]
        public static void LoginSystem()
        { /* ... */ }
    

    and go directly to the OnetimeTeardown method

    I updated my program and unloaded my project and removed references and elements related to coded ui and correctly added the three assembly references

    1. Microsoft.VisualStudio.TestTools.UITest.Playback
    2. Microsoft.VisualStudio.TestTools.UITest.WindowsStoreUtility
    3. Microsoft.VisualStudio.TestTools.UITesting

    After doing this my program worked as should.