質問

I have a simple NUnit project in C#. I have a file TestFixture.cs that successfully runs its four tests. If I want to add additional tests in a new CS file, how do I do this? 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. I would think they would automatically run. Do I need to tell the runner specifically if it is a file other than 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). Then NUnit will search for methods marked with Test or TestCase attributes and load them. If NUnit don't see some test, then make sure you loaded latest version of your test assembly.

NOTE: If you are using NUnit test runner, then there is nice setting Reload when test assembly changes. With this option turned on NUnit will automatically reload test assembly when you rebuild it in Visual Studio.

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top