Instrument inheritance inheritance and ignored basic test instruments

I have a set of basic tests that are used to test several implementations of an interface. The way I worked was to create a basic text element with the [Ignore] attribute.

[TestFixture] [Ignore] public class BaseTests { // Use your imagination for the actual name public virtual ITestableThing GetConcrete() { return null; } // All of my unit tests here } 

And then I write a subclass for each interface implementation:

 public class ConcreteThingTests : BaseTests { public override ITestableThing GetConcrete() { return new ConcreteThing(); } } 

This works well, since I have all the tests for ALL implementations in one place, and the subclasses simply define the implementation.

The problem is that I have to put the [Ignore] attribute in the base class, or NUnit will try to run the tests (and crash).

Because of this, my test results are always clogged with a set of tests with ignoring, and although this is not very important, I thought that for this there might be a better template that avoids ignoring the tests.

So am I implementing inheritance inheritance incorrectly?

+6
source share
2 answers

Typically, you set test attributes to specific test classes, not the base class.

Since you seem to be testing the same functionality for multiple classes, you can skip the entire test hierarchy and introduce specific classes that will be tested for this test base class.

To do this with NUnit, you can use the TestCaseSource attribute using the factory class method as a parameter. An example of this can be found here: How to pass dynamic objects to the NUnit TestCase function?

When compiling code for your specific case, it may look like this:

 /// <summary> /// Earlier known as your BaseTests class /// </summary> [TestFixture] public class TestOfConcreteImplementationsOfInterface { [TestCaseSource("CreateConcretes")] [Test] public void VerifyImplementations(IWhatever thing) { int input = 42; int result = thing.DoSomething(input); Assert.That(result, Is.EqualTo(input)); } /// <summary> /// Factory method for the concrete classes. If you want this in a seperate class, you can do that too using the /// ctor public TestCaseSourceAttribute(Type sourceType, string sourceName); /// </summary> public IEnumerable<IWhatever> CreateConcretes { get { yield return new A(); yield return new B(); } } } public interface IWhatever { int DoSomething(int x); } public class A : IWhatever { public int DoSomething(int x) { return x; } } public class B : IWhatever { public int DoSomething(int x) { return x; } } 
+4
source

The NUnit tester seems to ignore the base class if it is marked with an abstract:

 public abstract class BaseTests { } 
+13
source

Source: https://habr.com/ru/post/950966/


All Articles