"inherit" 可以用 xUnit.net 进行测试吗?
Is it possible to "inherit" tests with xUnit.net?
我有一个名为 EventManager 的具体 class 和一个名为 ScheduledEventManager 的子class。我希望 ScheduledEventManager 必须通过与 EventManager 相同的测试以及一些额外的测试。 xUnit.net 这可能吗?
编辑:我刚刚意识到我的情况比这复杂一点。我正在使用嵌套的 classes 来使我的测试更有条理。示例:
public class EventManagerTests
{
public class WhenAnEventIsFired
{
[Fact]
void ItNotifiesSubscribers()
{
// Perform the test
}
}
}
public class ScheduledEventManagerTests
{
// How to I inherit the above tests since they are in nested classes?
}
在我看来这是不可能的,但也许你们中的一位天才知道一些我不知道的事情。
创建一个参数化测试,将您的基础 class 的实例作为 SUT,并使用子 class 的实例调用该测试。这是一个使用 NUnit 的(人为的)示例,它导致一次通过和一次失败测试:
public class Foo
{
public virtual int DoSomething()
{
return 10;
}
}
public class Bar : Foo
{
public override int DoSomething()
{
return 9;
}
}
[TestFixture]
public class Tests
{
private Foo[] _foos = { new Foo(), new Bar() };
[Test]
[TestCaseSource("_foos")]
public void When_DoSomething_Is_Invoked_Then_A_Power_Of_Ten_Is_Returned(Foo sut)
{
Assert.That(sut.DoSomething() % 10, Is.EqualTo(0));
}
}
是的你可以:
public abstract class EventManagerTests
{
protected IEventManager _ev;
protected EventManagerTests(IEventManager ev)
{
_ev = ev;
}
[Fact]
public void SharedTest()
{
// Perform _ev test
}
}
public class ScheduledEventManagerTests : EventManagerTests
{
public ScheduledEventManagerTests():base(new ScheduledEventManager())
{
}
// It will inherit tests from the base abstract class
}
public class UnScheduledEventManagerTests : EventManagerTests
{
public UnScheduledEventManagerTests():base(new UnScheduledEventManager())
{
}
// It will inherit tests from the base abstract class
}
我有一个名为 EventManager 的具体 class 和一个名为 ScheduledEventManager 的子class。我希望 ScheduledEventManager 必须通过与 EventManager 相同的测试以及一些额外的测试。 xUnit.net 这可能吗?
编辑:我刚刚意识到我的情况比这复杂一点。我正在使用嵌套的 classes 来使我的测试更有条理。示例:
public class EventManagerTests
{
public class WhenAnEventIsFired
{
[Fact]
void ItNotifiesSubscribers()
{
// Perform the test
}
}
}
public class ScheduledEventManagerTests
{
// How to I inherit the above tests since they are in nested classes?
}
在我看来这是不可能的,但也许你们中的一位天才知道一些我不知道的事情。
创建一个参数化测试,将您的基础 class 的实例作为 SUT,并使用子 class 的实例调用该测试。这是一个使用 NUnit 的(人为的)示例,它导致一次通过和一次失败测试:
public class Foo
{
public virtual int DoSomething()
{
return 10;
}
}
public class Bar : Foo
{
public override int DoSomething()
{
return 9;
}
}
[TestFixture]
public class Tests
{
private Foo[] _foos = { new Foo(), new Bar() };
[Test]
[TestCaseSource("_foos")]
public void When_DoSomething_Is_Invoked_Then_A_Power_Of_Ten_Is_Returned(Foo sut)
{
Assert.That(sut.DoSomething() % 10, Is.EqualTo(0));
}
}
是的你可以:
public abstract class EventManagerTests
{
protected IEventManager _ev;
protected EventManagerTests(IEventManager ev)
{
_ev = ev;
}
[Fact]
public void SharedTest()
{
// Perform _ev test
}
}
public class ScheduledEventManagerTests : EventManagerTests
{
public ScheduledEventManagerTests():base(new ScheduledEventManager())
{
}
// It will inherit tests from the base abstract class
}
public class UnScheduledEventManagerTests : EventManagerTests
{
public UnScheduledEventManagerTests():base(new UnScheduledEventManager())
{
}
// It will inherit tests from the base abstract class
}