Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test specific base class? #2318

Closed
Thaina opened this issue Jul 12, 2017 · 5 comments
Closed

How to test specific base class? #2318

Thaina opened this issue Jul 12, 2017 · 5 comments

Comments

@Thaina
Copy link

Thaina commented Jul 12, 2017

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?

@jnm2
Copy link
Contributor

jnm2 commented Jul 12, 2017

Hey @Thaina! We were just talking about this. Here is what I would recommend you do: #2309 (comment) Does that suit your needs?

@jnm2 jnm2 added this to the Closed Without Action milestone Jul 12, 2017
@jnm2 jnm2 closed this as completed Jul 12, 2017
@jnm2
Copy link
Contributor

jnm2 commented Jul 12, 2017

Oops, wrong button. 😛

@jnm2 jnm2 reopened this Jul 12, 2017
@Thaina
Copy link
Author

Thaina commented Jul 12, 2017

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

@Thaina Thaina closed this as completed Jul 12, 2017
@jnm2
Copy link
Contributor

jnm2 commented Jul 12, 2017

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.

@CharliePoole
Copy link
Contributor

@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...

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:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants