有没有办法为这样的nunit安装方法添加参数:public void SetUp(Point p = null) { /*code*/ }.
我试过它并得到以下异常 SetUp : System.Reflection.TargetParameterCountException : Parameter count mismatch
我认为你的观点是避免代码重复。尝试使用 SetUp() 中使用的覆盖方法提取基类。所有派生类都将从基类执行测试,并在覆盖 OnSetUp() 中准备对象
[TestFixture]
public class BaseTestsClass
{
//some public/protected fields to be set in SetUp and OnSetUp
[SetUp]
public void SetUp()
{
//basic SetUp method
OnSetUp();
}
public virtual void OnSetUp()
{
}
[Test]
public void SomeTestCase()
{
//...
}
[Test]
public void SomeOtherTestCase()
{
//...
}
}
[TestFixture]
public class TestClassWithSpecificSetUp : BaseTestsClass
{
public virtual void OnSetUp()
{
//setup some fields
}
}
[TestFixture]
public class OtherTestClassWithSpecificSetUp : BaseTestsClass
{
public virtual void OnSetUp()
{
//setup some fields
}
}
Run Code Online (Sandbox Code Playgroud)
使用参数化 TestFixture 也很有用。课堂上的测试也将按照 TestFixture、SetUp 方法进行。但请记住
参数化装置(正如您所发现的)受到以下事实的限制:您只能使用属性中允许的参数
用法:
[TestFixture("some param", 123)]
[TestFixture("another param", 456)]
public class SomeTestsClass
{
private readonly string _firstParam;
private readonly int _secondParam;
public WhenNoFunctionCodeExpected(string firstParam, int secondParam)
{
_firstParam = firstParam;
_secondParam = secondParam;
}
[Test]
public void SomeTestCase()
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2501 次 |
| 最近记录: |