I have to convert NUnit 2.x tests to NUnit 3.x tests and currently have the situation that there is one base class with tests and abstract IEnumerable
property used in ValueSource
for these tests and many classes that inherits this base class and overrides this property.
The question is how to convert these tests to NUnit 3.x where ValueSource
has to be static. Each base child also has different TestCategory.Subcategory
.
[Category(TestCategory.Transformations)]
public abstract class Transformations
{
[Test]
public void TransformTest([ValueSource("TestDataSource")] TransformTestSource source)
{
// some test logic
}
protected abstract IEnumerable<TransformTestSource> TestDataSource { get; }
}
[TestFixture]
[Category(TestCategory.Transformations)]
[Category(TestCategory.Subcategory.Example1)]
public class ChildExample1
{
protected override IEnumerable<TransformTestSource> TestDataSource
{
get { /* get data for example from database, or file */ }
}
}
The only way in my mind is to remove abstract property definition from abstract class and define this property in each child class but it sounds awful. Is there a better way?
EDIT: Sometimes, there are also some other tests in child class, so those classes are there not always only for data fill.
The base class must be abstract else NUnit will instantiate it and run the tests it contains. Of course it will also re-run the same tests when it instantiates the base class. This is at best confusing and at worst will give errors when the tests are run on the base class alone.
You use the term dynamic in the question without explanation. If what you mean by that is an instance method or property, then this answer is for you. Of course, the contrary of a static method is an instance method in C# and "dynamic" is something else. In any case, if you do mean something else, please edit your question to explain.
So... while NUnit 3 requires your source to be static, that doesn't limit you to only using things that are known at compile time. If your source is a static method rather than a field, then it's possible for you to discover the necessary information to generate the data (in some sense) "dynamically."
It is also possible in NUnit 3 to use TestCaseSource
to contain your data. In that case, Form 3 of the attribute constructor (see the docs) does allow use of an instance member. This form, however, puts your data in a separate class from your tests and may not suit your usage.