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