Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nunit base class with common tests

Tags:

c#

nunit

I am using nunit to do some tests for some classes.

There are a couple of operations that are common for all the test classes but require different parameters to work.

So I added the tests in a base class and virtual methods in the base class to provide the parameters.

In the derived test classes I overriden the virtual methods to provide specific parameters for the tests in the base class.

Now my problem is that I want the tests in the base class to be executed only from the derived classes. I am currently using the ignore attribute on the base class to ignore the tests but this causes some warnings when the tests are ran and there is a policy that does not allow me to submit the changes to svn if there are a number of ignored tests.

So how can I run the tests from the base class in the derived classes only without using the ignore attribute on the base class.

like image 957
Alecu Avatar asked Feb 19 '14 12:02

Alecu


People also ask

Is TestFixture needed?

The TestFixture attribute is required however for parameterized or generic test fixture because in that case you also specify additional information through the attribute (parameters/concrete types).

Does SetUp run before every test NUnit?

Inheritance. The SetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetUp method, that method will be called before each test method in the derived class.

What is difference between NUnit and MsTest?

MsTest is a native unit testing library that comes with Visual Studio from Microsoft. NUnit is an extra Nuget package that needs to be installed on top and interact with the APIs of Visual Studio. Nunit likely doesn't have direct integration into the native APIs like MsTest does.

What is NUnit explain NUnit test and TestFixture?

The [TestFixture] attribute denotes a class that contains unit tests. The [Test] attribute indicates a method is a test method. Save this file and execute the dotnet test command to build the tests and the class library and run the tests. The NUnit test runner contains the program entry point to run your tests.


1 Answers

You should be able to mark your base class as abstract, this will stop nunit running the tests in that class - meaning you no longer need the ignore attribute.

namespace MyTests
{
    [TestFixture]
    public abstract class BaseTestClass
    {
        [Test]
        public void CommonTest()
        {

        }
    }

    [TestFixture]
    public class TestClass1 : BaseTestClass
    {
        [Test]
        public void OtherTest1()
        {

        }
    }

    [TestFixture]
    public class TestClass2 : BaseTestClass
    {
        [Test]
        public void OtherTest2()
        {

        }
    }
}
like image 178
ajg Avatar answered Sep 21 '22 08:09

ajg