Nunit:是否可以让测试出现嵌套
我想测试一种具有高圈复杂度的方法(叹息),并且我希望在测试类中包含一个类,以便方法测试类显示为树中的节点。 Nunit 可以吗?如何实现?
MyEntityTests
|
L_ MyComplexMethodTests
L when_some_condition_than
L when_some_other_condition_than
[TestFixture]
public class MyEntityTests
{
[TestFixture]
public class MyComplexMethodTests
{
[Test]
public void when_some_condition_than() {}
etc.....
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用嵌套类来完成此操作,与问题中的示例代码非常相似。
与您的代码的唯一区别是,如果外部类仅用于结构且本身没有测试,则不需要
[TestFixture]
属性。您还可以让所有内部类共享一个
Setup
方法,方法是将其放入外部类中并让内部类从外部类继承:在 NUnit GUI 中,此测试类将如下所示:
You can do it with nested classes, very similar to the example code in your question.
The only difference to your code is that the outer class doesn't need the
[TestFixture]
attribute if it's only used for the structure and doesn't have tests itself.You can also have all inner classes share a
Setup
method, by putting it into the outer class and having the inner classes inherit from the outer class:In the NUnit GUI, this test class will look like this:
我已经使用(滥用?)命名空间来获得此行为:
这将为您提供:
在这种情况下,我通常会使用 TestFixture 来定义测试的上下文。
I've used (abused?) namespaces to get this behavior:
Which will give you:
In this case I'll normally use the TestFixture to define the context for the test.
听起来您想要测试一个类,但您有两组/类型的测试要运行。最简单的方法可能是创建两个 TestFixture,每个 TestFixture 一个。另一种方法是将每个测试放入一个类别中。
编辑:如果所有测试都使用相同的方法,一种选择是使用 TestCase 属性并指定每个测试的参数(以及预期结果)。GUI 会将每组 TestCase 参数嵌套在该测试名称的单个实例下。这假设您的所有测试都将表现相似,这意味着相同的基本断言或预期异常。
It sounds like you have one class you want to test, but you have two sets/types of tests to run. The easiest way to do that might be to create two TestFixtures, one for each. Another way to do it is to place each test into a Category.
Edit: If all of the tests are on the same method, one option is to use the TestCase attribute and specify the parameters for each test (as well as the expected result.) The GUI will nest each set of TestCase parameters under a single instance of that test name. This assumes all of your tests will behave similarly, meaning the same basic Asserts or ExpectedExceptions.