从NUnit中的多个类文件运行TestFixtures

时间:2014-05-14 20:59:44

标签: c# unit-testing 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();
    }
}
}

1 个答案:

答案 0 :(得分:1)

当您加载时,NUnit会自动从测试程序集中获取所有标有TestFixture属性的类型(无论是一个 .cs 文件中的灯具还是单独的)。然后,NUnit将搜索标有TestTestCase属性的方法并加载它们。如果NUnit没有看到一些测试,那么请确保加载了测试程序集的最新版本。

注意:如果您使用的是NUnit测试运行器,那么在测试程序集更改时重新加载会有很好的设置。启用此选项后,NUnit将在Visual Studio中重建时自动重新加载测试程序集。

enter image description here