繁体   English   中英

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

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

为了在NUnit中使用调试模式,我添加了一个在线模板“ NUnit Test application”。 因此,当我添加一个新项目时,我选择了NUnit测试应用程序而不是类库。 创建项目时,将自动添加两个.cs文件。 我添加了一个简单的程序来检查调试模式,它显示了一个错误。 如何纠正此错误? 谢谢。

TypeInitializationException was unhandled.

错误发生在

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

自动添加的文件为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);
      }
    }
  }

我向其中添加了一个新的项目类,并尝试调试

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();
        }
      }
   }

正如@Arran已经提到的那样,您真的不需要执行所有这些操作。 但是您可以使调试NUnit测试更加容易。

在Visual Studio中使用F5调试单元测试

与其执行NUnit运行程序并使用Visual Studio附加到进程,不如配置您的测试项目以启动NUnit测试运行程序并调试测试。 您所要做的就是遵循以下步骤:

  1. 打开测试项目的属性
  2. 选择调试选项卡
  3. 开始操作设置为启动外部程序并指向NUnit运行器
  4. 设置命令行参数
  5. 保存项目属性

这样就完成了。 F5键 ,您的测试项目将以NUnit运行程序执行的调试模式启动。

您可以在我的博客文章中阅读有关内容。

您根本不需要做所有这一切。

打开NUnit GUI,打开已编译的测试。 在Visual Studio中,使用“ Attach to Process功能附加nunit-agent.exe。

在NUnit GUI中运行测试。 VS调试器将从那里获取它。

您正在花费太多的精力来完成此任务。

我通常要做的是创建一个新的“类库”项目。 然后,将对nunin-framework.dll的引用添加到我的项目中。

您可以如下定义类:

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

    }
}

TestFixture属性在此处描述

然后,您可以继续使用上述公共方法创建多个测试。

要使之正常工作,有3件事非常重要。

  1. 您需要将项目文件上的调试器设置为外部可执行文件,即nunint.exe。
  2. 传递的参数必须是程序集的名称。
  3. 如果使用的是.net 4.0,则需要在nunint.exe.config中指定。如果不这样做,则将无法使用VS进行调试。 请参阅下面的配置片段:

     <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> 

希望这会有所帮助

暂无
暂无

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

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