简体   繁体   English

如何在Visual Studio中以调试模式运行NUnit?

[英]How to run NUnit in debug mode from Visual Studio?

To use the debug mode in NUnit I added an online template "NUnit Test application". 为了在NUnit中使用调试模式,我添加了一个在线模板“ NUnit Test application”。 So when I add a new project I choose NUnit test application instead of a class library. 因此,当我添加一个新项目时,我选择了NUnit测试应用程序而不是类库。 When the project gets created two .cs files gets added automatically. 创建项目时,将自动添加两个.cs文件。 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 自动添加的文件为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 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();
        }
      }
   }

As already mentioned by @Arran, you really don't need to do all this. 正如@Arran已经提到的那样,您真的不需要执行所有这些操作。 But you can make it even easier to debug NUnit tests. 但是您可以使调试NUnit测试更加容易。

Using F5 in Visual Studio to debug unit tests 在Visual Studio中使用F5调试单元测试

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. 与其执行NUnit运行程序并使用Visual Studio附加到进程,不如配置您的测试项目以启动NUnit测试运行程序并调试测试。 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 开始操作设置为启动外部程序并指向NUnit运行器
  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. F5键 ,您的测试项目将以NUnit运行程序执行的调试模式启动。

You can read about this in my blog post . 您可以在我的博客文章中阅读有关内容。

You don't need to do all this at all. 您根本不需要做所有这一切。

Open the NUnit GUI, open up your compiled tests. 打开NUnit GUI,打开已编译的测试。 In Visual Studio, use the Attach to Process feature to attach the nunit-agent.exe. 在Visual Studio中,使用“ Attach to Process功能附加nunit-agent.exe。

Run the tests in the NUnit GUI. 在NUnit GUI中运行测试。 The VS debugger will take it from there. VS调试器将从那里获取它。

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. 然后,将对nunin-framework.dll的引用添加到我的项目中。

You can define your class as follows: 您可以如下定义类:

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

    }
}

The TestFixture attribute is described here TestFixture属性在此处描述

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. 要使之正常工作,有3件事非常重要。

  1. You need to set your debugger on your project file to an external executable, ie nunint.exe 您需要将项目文件上的调试器设置为外部可执行文件,即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. 如果使用的是.net 4.0,则需要在nunint.exe.config中指定。如果不这样做,则将无法使用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 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM