簡體   English   中英

從NUnit中的多個類文件運行TestFixtures

[英]Running TestFixtures from multiple class files in NUnit

我在C#中有一個簡單的NUnit項目。 我有一個文件TestFixture.cs,可以成功運行其四個測試。 如果要在新的CS文件中添加其他測試,該怎么做? 如果僅將代碼從TestFixture.cs復制到新的TestFixture2.cs中,並將TestFixture類分別重命名為TestFixture3和4,則它們將不會執行。 我認為它們會自動運行。 我是否需要告訴跑步者它是否是TestFixture.cs以外的文件? 這是我的代碼:

namespace NUnitTest1
{
[TestFixture]
public class TestFixture3
{
    [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(true);
    }
}

[TestFixture]
public class TestFixture4
{
    [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(true);
    }
}

}

我的單元測試通過使用以下代碼執行命令行程序來運行:

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

當您加載NUnit時,NUnit會自動從測試程序集中獲取所有帶有TestFixture屬性標記的類型(無論夾具是位於一個.cs文件中還是位於單獨的文件中)。 然后,NUnit將搜索標記有TestTestCase屬性的方法並加載它們。 如果NUnit沒有看到任何測試,請確保已加載最新版本的測試程序集。

注意:如果您使用的是NUnit測試運行器,則有一個不錯的設置, 當測試組件更改時,重新加載 啟用此選項后,在Visual Studio中重建NUnit時,它將自動重新加載測試程序集。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM