基类中的NUnit和
我正在使用NUnit查看一些测试代码,NUnitinheritance自包含[SetUp]属性的基类:
public class BaseClass { [SetUp] public void SetUp() { //do something } } [TestFixture] public class DerivedClass : BaseClass { [SetUp] public void SetUp() { //do something else, with no call to base.SetUp() } //tests run down here. //[Test] //[Test] //etc }
派生类肯定需要在基类’SetUp()方法中完成的工作。
我是否遗漏了某些内容,或者在运行派生类的测试时是否会调用基类中的SetUp()方法? [SetUp]属性有什么特别之处可以确保在另一个之前调用一个属性吗?
您只能有一个SetUp
方法。
TestFixture只能有一个SetUp方法。 如果定义了多个,则TestFixture将成功编译,但其测试将不会运行。
https://www.nunit.org/index.php?p=setup&r=2.2.10
如果需要在子类中添加其他设置逻辑,请在父类中将SetUp
标记为虚拟,覆盖它,如果希望基类的设置也运行,则调用base.SetUp()
。
public class BaseClass { [SetUp] public virtual void SetUp() { //do something } } [TestFixture] public class DerivedClass : BaseClass { public override void SetUp() { base.SetUp(); //Call this when you want the parent class's SetUp to run, or omit it all together if you don't want it. //do something else, with no call to base.SetUp() } //tests run down here. //[Test] //[Test] //etc }
在NUnit 2.5之前,之前的答案是正确的; 你只能有一个[SetUp]
属性用于测试。
使用NUnit 2.5以后,您可以使用[SetUp]
属性修饰多个方法。 因此,以下在NUnit 2.5+中完全有效:
public abstract class BaseClass { [SetUp] public void BaseSetUp() { Debug.WriteLine("BaseSetUp Called") } } [TestFixture] public class DerivedClass : BaseClass { [SetUp] public void DerivedSetup() { Debug.WriteLine("DerivedSetup Called") } [Test] public void SampleTest() { /* Will output * BaseSetUp Called * DerivedSetup Called */ } }
inheritanceNUnit时,总是首先在基类中运行'[SetUp]’方法。 如果在单个类中声明了多个[SetUp]
方法,则NUnit无法保证执行的顺序。
有关详细信息,请参见此处
上述就是C#学习教程:基类中的NUnit和分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/962378.html