Nunit: How to test specific base class?

Created on 12 Jul 2017  ·  5Comments  ·  Source: nunit/nunit

I have a class want to test that was inherited in the same assembly. That class has a test function that need to test just on the base class. I want the test to only run once, not run on inherited

How could I do?

duplicate

All 5 comments

Hey @Thaina! We were just talking about this. Here is what I would recommend you do: https://github.com/nunit/nunit/issues/2309#issuecomment-313936931 Does that suit your needs?

Oops, wrong button. 😛

@jnm2 Well, not so wrong because it is. But I just think it should be natively support though

Thanks for letting us know your opinion. I think you're only the second person to ask for it so far, but we'll reconsider if enough people end up asking.

@Thaina You are trying to do something that NUnit is not designed to do, so any change request is a request for a change in how NUnit fundamentally works. That's not to say it's impossible, but it means (1) you need some good reasons why we might do it and (2) you should at least consider how the designers of NUnit thought this kind of situation ought to work.

If I understand what you are saying, you have a test written like this...

```C#
public class BaseClass
{
// Maybe common setup and teardown and other tests

[Test]
public void SomeTest() { }

}

public class DerivedClass : BaseClass
{
// This class runs it's own tests plus SomeTest from base
}

`SomeTest` runs on both the base class and the derived class. You want it to run on only the base class. According to the intended design of NUnit, we would expect your base class to be abstract and we would run it only on the __derived__ class. However, you want to run it only on the __base__ class. In order to do that, our standard recipe would be as follows:

```C#
public abstract class BaseClass
{
    // Maybe common setup and teardown and other tests
}

public class DerivedClass1 : BaseClass
{
    // This class runs it's own tests plus SomeTest from base
}

public class DerivedClass2 : BaseClass
{
    [Test]
    public void SomeTest() { }
}

In this design, the base class is never executed directly. It participates through inheritance in the execution of the two derived classes. This is not, obviously, the only way it could have been done, but it is how NUnit was designed to work. Base classes are intended to be abstract and not to supply any tests directly. They only supply them to the classes that derive from them.

This is a description of how NUnit works, right now. If you can fit into this pattern, life will be easier, even though you can still ask for us to change things at some point.

Was this page helpful?
0 / 5 - 0 ratings