2

OneTimeSetUp and OneTimeTearDownStopped working

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{

    class Class1
    {
        [SetUp]
        public void setup()
        {
            Console.WriteLine("setup method");
        }
        [TearDown]
        public void teardown()
        {
            Console.WriteLine("teardown");
        }
        [OneTimeSetUp]
        public void onetimesetup()
        {
            Console.WriteLine("onetimesetup method");
        }
        [OneTimeTearDown]
        public void onetimeteardown()
        {
            Console.WriteLine("onetimeteardown method");
        }
        [Test]
        public void testCase()
        {
            Console.WriteLine("Testcase method 2");
        }

    }
}

I installed Resharper previously and Now I have completed the resharper. I have deleted resharper from C:\Users\vkris\AppData\Local\Microsoft\VisualStudio\15.0_d20cab45\Extensions Also I now more have it in the control panel.

But I am still I having this issues. I have a previous project which is working fine. Here are the packages that I have downloaded from nuget package. More precisely please find the project which I am trying to complete

https://github.com/vkrishna92/AutomationProjects/blob/master/SeleniumAutomationDemoQA/SeleniumAutomationDemoQA/Utitlities/OneTimeClass.cs

enter image description here

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
user9782383
  • 23
  • 1
  • 5
  • 1
    Why the xunit tag? This seems to be an NUnit question. – Richardissimo May 27 '18 at 06:19
  • This isn't a great question. It isn't clear how you are running the tests, what result you are getting and what result you are expecting. My observations are that the class is missing a TestFixtureAttribute, and the the OneTime attributes were only [introduced as a rename from the old names in v3](https://github.com/nunit/docs/wiki/SetUp-and-TearDown-Changes). Is it possible you are using a test runner (e.g. NUnit console) from the wrong version of NUnit, which doesn't recognise those new names? – Richardissimo May 27 '18 at 06:29
  • I tried to run from Test explorer from visual studios. Im expecting this output onetimesetup method-->setup method-->Testcase method 2 --> teardown--> onetimeteardown method . But Output is setup method-->Testcase method 2 --> teardown . I also tried testFixture attribute to the class – user9782383 May 28 '18 at 02:12
  • My guess is that it's using the wrong version. Try renaming the attribute "OneTimeSetUp" to be "TestFixtureSetUp" and see if that makes it run that method. If that works, then you need to work out why it's using an old version of NUnit when running the tests. – Richardissimo May 28 '18 at 15:11
  • Hello Richard. I tried what you suggested. But it did not work. I have move to Nunit2 and It worked partially. It did not invoke the TestFixtureTearDown. I am still unable to use OneTimeSetup and OneTimeTearDown in Nunit3. I formatted my system and re installed everything But no change in result. Kindly please help – user9782383 Jun 01 '18 at 01:27
  • I think this is a problem with the integration between Visual Studio Test Runner and the NUnit 3 Test Adapter extension for Visual Studio. IT USED TO WORK! – jmbmage Sep 20 '18 at 15:40

1 Answers1

1

There's nothing wrong with the TestFixture (for example, here's a similar question), so it must be a problem with the way the tests are being run, so let me offer an alternative way for you to run the tests...

  1. Get the NUnit.Console NuGet package (this includes another package called NUnit.ConsoleRunner, referred to later).
  2. Choose a project containing the Unit Tests that you want to run.
  3. Go to the project properties of that project.
  4. Go to the "Application" tab.
  5. Select the assembly name and copy it to the clipboard.
  6. Go to the "Debug" tab.
  7. In "Start Options"/"Command line arguments:" paste the assembly name and add ".dll --debug --inprocess" on the end.
  8. Change "Working directory" to the folder where that DLL gets built to.
  9. Set "Start Action" to "Start external program", and give the location of the NUnit console (for example "full path\NUnit.ConsoleRunner.version\tools\nunit3-console.exe").
  10. In "Solution Explorer", right-click on the project and choose "Set as StartUp Project".
  11. Then "Start" the solution from Visual Studio

Hope this helps.

Richardissimo
  • 5,596
  • 2
  • 18
  • 36
  • I followed the steps you suggested me. But it did not work out. Is this anything related to Visual studios version. I am using VS 2017 with latest updates . Thanks for trying to help me out. – user9782383 Jun 02 '18 at 18:41
  • The steps above should be fine for about the last 4 major versions of Visual Studio. You need to help me to help you: "it did not work out" gives me no clue what problem occurred. – Richardissimo Jun 02 '18 at 20:39
  • Sorry about my incomplete information. I tried once again with your given steps. I found out where I made the mistake. I made mistake in the 7th step. And this time it worked. OnetimeSetup and OneTimeTearDown are being invoked. But I have a question. I am still unable to Invoke OnetimeSetup and OneTimeTearDown from TestExplorer. It works complete fine when I run it from "start " button. Why is it like this. Is this same to everyone or am I making any mistake. Also If I have to run individual testcases how can I achieve this with "Start" button.? Above all you resolution was really helpful. – user9782383 Jun 03 '18 at 18:43
  • That seems to confirm my suspicion that the Visual Studio test runner you're using is somehow picking up a pre-v3 version of NUnit. No, it's not meant to be like that. How to run the tests individually: There is a GUI version of the Test Runner, which is in pre-release after being completely re-written for v3, for which I refer you to [this accepted answer from RobProuse](https://stackoverflow.com/a/35142848/5198140). – Richardissimo Jun 03 '18 at 19:44