【问题标题】:How should I group unit tests using C# and NUnit?我应该如何使用 C# 和 NUnit 对单元测试进行分组?
【发布时间】:2010-11-03 21:56:39
【问题描述】:

我有一个在 C# 中包含大量单元测试的类(使用 NUnit 2.5.8),我想根据我正在测试的类功能的哪个区域将单元测试组合在一起(这样我就可以快速选择要在 NUnit UI 中运行的集合)。

我知道我可以将类重构为更小的组件,这样可以解决问题,但有没有其他方法可以在不完全重新设计生产代码的情况下做到这一点?

【问题讨论】:

  • 为什么你的单元测试的布局取决于产品代码的设计方式???
  • @Pieter:按照惯例,我总是每个生产类都有一个 TestFixture。正如 Jason 的回答中所解释的那样,显然有更好的做事方式。

标签: c# unit-testing nunit


【解决方案1】:

为什么不为您要测试的每个功能区域提供[TestFixture](单独的类)?

class ClassThatDoesTooMuch {
    // functionality related to opening a database connection
    // functionality related to file management
    // functionality related to solving world hunger
}

[TestFixture]
public class ClassThatDoesTooMuchDatabaseConnectionTests { // }

[TestFixture]
public class ClassThatDoesTooMuchFileManagementTests { // }

[TestFixture]
public class ClassThatDoesTooMuchWorldHungerSolutionTests { // }

【讨论】:

  • 很明显我很尴尬自己没有想到它。 摇晃
  • 此外,您可以在每个测试装置上使用[Category("ClassThatDoesTooMuch")],这样您仍然可以同时运行与ClassThatDoesTooMuch 相关的所有测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 2011-01-25
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
相关资源
最近更新 更多