简体   繁体   English

获取通用单元测试的通用测试数据

[英]Getting generic test data for generic unit tests

I want to reuse generic tests, but how can I get generic test data? 我想重用通用测试,但是如何获得通用测试数据?


I wrote my own IReadOnlyCollection<T> interface, and wrote some classes that use it. 我编写了自己的IReadOnlyCollection<T>接口,并编写了一些使用它的类。

Since the methods and properties (eg Contains , CopyTo ) of that interface should always work exactly the same regardless of the class that implements it, I want to write generic tests that I can apply to any implementation. 由于该接口的方法和属性(例如, ContainsCopyTo )应始终完全相同,而与实现该接口的类无关,因此,我想编写可应用于任何实现的通用测试。 Using the approach suggested in this post , I now have the following: 使用建议的方法这个帖子 ,我现在有以下几点:

// Tests that must work for any type T:
public abstract class IReadOnlyCollectionTests<T>
{
    protected abstract IReadOnlyCollection<T> CreateInstance(params T[] data);

    [Test]
    public void Contains_GivenExistingValue_ReturnsTrue()
    {
        // Given
        T[] data;   // <-- Get some data?
        T value = data[1];
        var sut = CreateInstance(data);

        // When
        bool result = sut.Contains(value);

        // Then
        Assert.IsTrue(result);
    }

    // 40 more such tests...
}

Now I need some data to test with. 现在,我需要一些数据进行测试。 Type T may be boolean, or string, or anything. 类型T可以是布尔值,字符串或其他任何类型。 How do I get some generic data in there that works for any type T ? 如何在其中获取适用于任何T类型的通用数据?


By the way: I'll run this generic test by deriving a test class for each implementation, like this one for my BitArray implementation (a collection of booleans): 顺便说一句:我将通过为每个实现派生一个测试类来运行此通用测试,例如针对我的BitArray实现(一个布尔值的集合)的测试类:

[TestFixture]
public class BitArrayROC : IReadOnlyCollectionTests<bool>
{
    protected override IReadOnlyCollection<bool> CreateInstance(params bool[] data)
    {
        return new BitArray(data);
    }
}

Similar to BitArray , I have a StringCollection class (among others) for which I want to test the IReadOnlyCollection<T> implementation. BitArray相似,我有一个StringCollection类(除其他外),我想为其测试IReadOnlyCollection<T>实现。

Create data provider which return data by parameter, and let parameter be the type of the data you need. 创建数据提供程序,该提供程序按参数返回数据,并让parameter为所需数据的类型。 You could set the type of data you need in implementation of your generic test. 您可以设置实现通用测试所需的数据类型。

You can create abstract method which returns data in IReadOnlyCollection<T> class. 可以创建返回抽象方法dataIReadOnlyCollection<T>类。 Then in derived classes you can write type-specific implementations. 然后,可以在派生类中编写特定于类型的实现。

See example: 参见示例:

public abstract class IReadOnlyCollectionTests<T>
{
    protected abstract IReadOnlyCollection<T> CreateInstance(params T[] data);

    protected abstract T[] GetData();

    [Test]
    public void Contains_GivenExistingValue_ReturnsTrue()
    {
        // Given
        T[] data = GetData();
        T value = data[1];
        var sut = CreateInstance(data);

        ...
    }
}

[TestFixture]
public class BitArrayROC : IReadOnlyCollectionTests<bool>
{
    protected override bool[] GetData()
    {
        return new[] { true, false };
    }

    ...
}

Actually, the idea is the same as you've implemented for collection instance creation. 实际上,这个想法与您为集合实例创建所实现的想法相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM