简体   繁体   English

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

[英]Running TestFixtures from multiple class files in NUnit

I have a simple NUnit project in C#. 我在C#中有一个简单的NUnit项目。 I have a file TestFixture.cs that successfully runs its four tests. 我有一个文件TestFixture.cs,可以成功运行其四个测试。 If I want to add additional tests in a new CS file, how do I do this? 如果要在新的CS文件中添加其他测试,该怎么做? If I simply copy the code from TestFixture.cs into a new TestFixture2.cs and rename the TestFixture classes to TestFixture3 and 4 respectively, they do not execute. 如果仅将代码从TestFixture.cs复制到新的TestFixture2.cs中,并将TestFixture类分别重命名为TestFixture3和4,则它们将不会执行。 I would think they would automatically run. 我认为它们会自动运行。 Do I need to tell the runner specifically if it is a file other than TestFixture.cs? 我是否需要告诉跑步者它是否是TestFixture.cs以外的文件? Here is my code: 这是我的代码:

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

} }

My unit test runs by executing a command line program using the following code: 我的单元测试通过使用以下代码执行命令行程序来运行:

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 will automatically get all types marked with TestFixture attribute from test assembly when you load it (it does not matter whether fixtures in one .cs file or in separate). 当您加载NUnit时,NUnit会自动从测试程序集中获取所有带有TestFixture属性标记的类型(无论夹具是位于一个.cs文件中还是位于单独的文件中)。 Then NUnit will search for methods marked with Test or TestCase attributes and load them. 然后,NUnit将搜索标记有TestTestCase属性的方法并加载它们。 If NUnit don't see some test, then make sure you loaded latest version of your test assembly. 如果NUnit没有看到任何测试,请确保已加载最新版本的测试程序集。

NOTE: If you are using NUnit test runner, then there is nice setting Reload when test assembly changes . 注意:如果您使用的是NUnit测试运行器,则有一个不错的设置, 当测试组件更改时,重新加载 With this option turned on NUnit will automatically reload test assembly when you rebuild it in Visual Studio. 启用此选项后,在Visual Studio中重建NUnit时,它将自动重新加载测试程序集。

在此处输入图片说明

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

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