Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nunit 测试夹具继承和忽略的基本测试夹具_Nunit - Fatal编程技术网

Nunit 测试夹具继承和忽略的基本测试夹具

Nunit 测试夹具继承和忽略的基本测试夹具,nunit,Nunit,我有一组基本测试,用于测试接口的多个实现。我对此建模的方法是创建一个带有[Ignore]属性的基本文本固定装置 [TestFixture] [Ignore] public class BaseTests { // Use your imagination for the actual name public virtual ITestableThing GetConcrete() { return null; } // All

我有一组基本测试,用于测试接口的多个实现。我对此建模的方法是创建一个带有[Ignore]属性的基本文本固定装置

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

     // All of my unit tests here
}
然后我为每个接口实现编写一个子类:

public class ConcreteThingTests :  BaseTests
{
    public override ITestableThing GetConcrete()
    {
        return new ConcreteThing();
    }
}
这很好,因为我在一个地方对所有实现进行了所有测试,而子类只指定了实现

问题是我必须将[Ignore]属性放在基类上,否则NUnit将尝试运行测试(并失败)

正因为如此,我的测试结果总是被一组被忽略的测试弄得乱七八糟,虽然这没什么大不了的,但我认为有一种更好的模式可以避免忽略测试


那么,我是否实现了测试夹具继承错误?

您通常会在具体的测试类上设置测试属性,而不是基类

因为您似乎要为多个类测试相同的功能,所以可以跳过整个测试层次结构,将要测试的具体类注入到该测试基类中

要使用NUnit实现这一点,您可以使用TestCaseSource属性和类工厂方法作为参数。以下是一个例子:

为您的特定案例编写一些代码,可能如下所示:

/// <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;
    }
}
//
///早期称为BaseTests类
/// 
[测试夹具]
接口的混凝土实现的公共类测试
{
[TestCaseSource(“CreateConcretes”)]
[测试]
公共无效验证实现(IWhatever)
{
int输入=42;
int result=thing.DoSomething(输入);
Assert.That(result,Is.EqualTo(input));
}
/// 
///具体类的工厂方法。如果您希望在单独的类中使用此方法,也可以使用
///ctor公共TestCaseSourceAttribute(类型sourceType,字符串sourceName);
/// 
公共IEnumerable属性
{
得到
{
返回新的A();
收益返回新B();
}
}
}
公共接口IWhatever
{
int剂量测定法(int x);
}
公共A级:我曾经
{
公共整数剂量测量(整数x)
{
返回x;
}
}
B级公共课:IWhatever
{
公共整数剂量测量(整数x)
{
返回x;
}
}

如果基类标记为抽象,NUnit测试运行程序似乎会忽略基类:

public abstract class BaseTests
{
}

我遇到的问题是,它将基类与测试类紧密耦合。想要创建一个新的实现并根据规范测试它的人不需要修改基类来添加属性或更改方法。完美的我有一个抽象的测试夹具库,但当我创建了一个继承自它的二级夹具库时,NUnit开始抱怨它。将抽象添加到子类中(以前从未尝试过这样做),它就像一个符咒一样工作。Lee=theman(tm)。我认为它是来自名称空间之外的抽象类。当我把我的作为摘要复制到同一个项目中时,它起了作用,但当它们在不同的项目中时就不起作用了。努尼特版本也必须相同