gpt4 book ai didi

c# - 有没有办法用参数创建 nunit 安装程序

转载 作者:太空狗 更新时间:2023-10-29 19:47:08 26 4
gpt4 key购买 nike

有没有办法像这样向 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
}
}

使用参数化的 TestFixture 也很有用。类里面的测试将按 TestFixture 和 SetUp 方法进行。但是请记住

Parameterized fixtures are (as you have discovered) limited by the fact that you can only use arguments that are permitted in attributes

用法:

[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()
{
...
}
}

关于c# - 有没有办法用参数创建 nunit 安装程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15029756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com