Question

To use the debug mode in NUnit I added an online template "NUnit Test application". So when I add a new project I choose NUnit test application instead of a class library. When the project gets created two .cs files gets added automatically. I added a simple program to check the debug mode and it shows an error. How to rectify this error? Thanks.

TypeInitializationException was unhandled.

Error occurs at

int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);

The automatically added files are Program.cs

namespace NUnitTest1
{
    class Program
    {
       [STAThread]
       static void Main(string[] args)
       {
         string[] my_args = { Assembly.GetExecutingAssembly().Location };
         int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);

         if (returnCode != 0)
            Console.Beep();
       }
    }
 }

TestFixture.cs

namespace NUnitTest1
{
   [TestFixture]
   public class TestFixture1
    {
      [Test]
      public void TestTrue()
      {
        Assert.IsTrue(true);
      }

    // This test fail for example, replace result or delete this test to see all tests pass
      [Test]
      public void TestFault()
      {
        Assert.IsTrue(false);
      }
    }
  }

I added a new item class to it and tried to debug

namespace NUnitTest1
{
   [TestFixture]
    public class Class1
    {
        IWebDriver driver = null;
        [SetUp]
        public void setup()
        {
           //set the breakpoint here
            driver = new FirefoxDriver();
        }
        [Test]
        public void test1()
        {
            driver.Navigate().GoToUrl("http://www.google.com/");                
        }
        [TearDown]
        public void quit()
        {
            driver.Quit();
        }
      }
   }
Was it helpful?

Solution 2

You don't need to do all this at all.

Open the NUnit GUI, open up your compiled tests. In Visual Studio, use the Attach to Process feature to attach the nunit-agent.exe.

Run the tests in the NUnit GUI. The VS debugger will take it from there.

OTHER TIPS

As already mentioned by @Arran, you really don't need to do all this. But you can make it even easier to debug NUnit tests.

Using F5 in Visual Studio to debug unit tests

Instead of executing NUnit runner and attaching to the process using Visual Studio, it's better to configure yout test project to start the NUnit test runner and debug your tests. All you have to do is to follow these steps:

  1. Open test project's properties
  2. Select Debug tab
  3. Set Start action to Start external program and point to NUnit runner
  4. Set Command line arguments
  5. Save project properties

And you're done. Hit F5 and your test project will start in debug mode executed by NUnit runner.

You can read about this in my blog post.

You're going through way too much effort to get this done.

What I usually do is to go and create a new "Class Library" project. I then add a reference to the nunin-framework.dll onto my project.

You can define your class as follows:

[TestFixture]
public class ThreadedQuery
{
    [Test]
    public void Query1()
    {

    }
}

The TestFixture attribute is described here

You can then go ahead and create multiple Tests with public methods as above.

There are 3 things that are quite important to get this to work then.

  1. You need to set your debugger on your project file to an external executable, ie nunint.exe
  2. The arguments that are passed need to be the name of your assembly.
  3. If you're making use of .net 4.0, you need to specify that in your nunint.exe.config If you do not do this, you will not be able to debug using VS. See snippet of config below:

    <startup useLegacyV2RuntimeActivationPolicy="true">
        <!-- Comment out the next line to force use of .NET 4.0 -->
        <!--<supportedRuntime version="v2.0.50727" />-->
        <supportedRuntime version="v4.0.30319" />
        <supportedRuntime version="4.0" />
    </startup>
    

Hope this is helpful

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top