NUnit:如何通过非静态方法传递TestCaseData? [英] NUnit: How to pass TestCaseData from a non-static method?

查看:70
本文介绍了NUnit:如何通过非静态方法传递TestCaseData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的测试由于以下原因而失败: 消息:在TestCaseSourceAttribute上指定的sourceName必须引用静态字段,属性或方法.

my test fails because of : Message: The sourceName specified on a TestCaseSourceAttribute must refer to a static field, property or method.

这是我的代码:

const double MAX_DELTA = 0.01;
Qv_ges qv_ges_NE;
double Sum_Qv_ges_R_FL;
Qv_ges Qv_ges_Quer;

[SetUp]
public void init()
{
    qv_ges_NE = Din1946.Calc_Qv_ges_NE(205.7d);
    Sum_Qv_ges_R_FL = 15d + 15d + 15d + 15d + 15d + 10d + 10d + 10d + 10d + 10d + 10d + 10d;
    Qv_ges_Quer = Din1946.Calc_Qv_ges_Quer(qv_ges_NE, Sum_Qv_ges_R_FL);
}

public IEnumerable<TestCaseData> TestCases_A()
{
        yield return new TestCaseData(72.5, Qv_ges_Quer.FL.Value, MAX_DELTA);
        yield return new TestCaseData(169.17, Qv_ges_Quer.RL.Value, MAX_DELTA);
        yield return new TestCaseData(241.67, Qv_ges_Quer.NL.Value, MAX_DELTA);
        yield return new TestCaseData(314.17, Qv_ges_Quer.IL.Value, MAX_DELTA);
    }


    [TestCaseSource("TestCases_A")]
public void MethodA(double expected, double value, double latitude)
      { Assert.AreEqual(expected, value, latitude);}

我只使用了静态测试用例方法,但是现在我需要像这样的数据: Qv_ges_Quer.IL.ValueQv_ges_Quer.FL.Value ....所以我删除了静态

I used only static testcase-methods, but now I need data like: Qv_ges_Quer.IL.Value, Qv_ges_Quer.FL.Value.... so I deleted static

如何使用非静态测试用例?我还通过调试注意到它一开始并没有进入设置程序

How can I use a non static testcase? I also noticed by debugging that it doesn't enter the SetUp at first

这是我想重新组织的旧代码,我想您会发现上面的另一种/更好的方法:

This is my old code which I want to reorginze, mybe you know another/better way then the way above:

public void MethodA(){
  Qv_ges qv_ges_NE = Din1946.Calc_Qv_ges_NE(205.7d);

  double Sum_Qv_ges_R_FL = 15d + 15d + 15d + 15d + 15d + 10d + 10d + 10d + 10d + 10d + 10d + 10d;
  Qv_ges Qv_ges_Quer = Din1946.Calc_Qv_ges_Quer(qv_ges_NE, Sum_Qv_ges_R_FL);

  Assert.AreEqual(72.5, Qv_ges_Quer.FL.Value, MAX_DELTA);
  Assert.AreEqual(169.17, Qv_ges_Quer.RL.Value, MAX_DELTA);
  Assert.AreEqual(241.67, Qv_ges_Quer.NL.Value, MAX_DELTA);
  Assert.AreEqual(314.17, Qv_ges_Quer.IL.Value, MAX_DELTA);
}

推荐答案

根据设计,TestCaseSourceAttribute使用的方法,属性或字段必须是静态的.这样做是为了避免在加载测试时实例化夹具类.仅在我们开始运行时才实例化您的灯具-对于GUI,每次启动时-您的灯具的寿命仅与运行灯具所需的时间一样长.

By design, the method, property or field used by the TestCaseSourceAttribute must be static. This is intended to avoid the need to instantiate the fixture class at the time the tests are loaded. Your fixture is only instantiated when we start the run - in the case of the GUI, each time we start the run - and its lifetime is only as long as it takes to run the fixture.

在您的情况下,您似乎发现可以使用静态方法.如果可能的话,那是最好的.

In your case, you have appear to have discovered that you can use a static method. That's best, if possible.

在这里使用实例方法的唯一方法是使用构造函数TestCaseSourceAttribute(Type sourceType),其中sourceType实现IEnumerable并直接返回测试用例数据.如果使用此功能,建议您使用与TestFixture不同的类.这不是绝对必要的.如果使用相同的类,则将在加载时和运行时创建不同的实例,它们彼此之间没有任何联系.许多开发人员最终对此感到困惑,并试图在加载时将状态留给测试使用.那行不通.

The only way to use instance methods here is to use the constructor TestCaseSourceAttribute(Type sourceType) where sourceType implements IEnumerable and returns your test case data directly. If you use this, I recommend using a different class from your TestFixture. It's not absolutely necessary. If you use the same class, different instances will be created at load time and run time, which have no connection whatsoever with one another. Many developers end up getting confused by this and try to leave state behind at load time for use by the tests. That won't work.

这篇关于NUnit:如何通过非静态方法传递TestCaseData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆