Visual Studio C#单元测试-使用不同/多个测试初始化​​运行单元测试,多次运行同一单元测试? [英] Visual Studio C# unit testing - Run Unit test with varied/multiple test initializations, Run same unit test multiple times?

查看:246
本文介绍了Visual Studio C#单元测试-使用不同/多个测试初始化​​运行单元测试,多次运行同一单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是这样:

  1. 创建一堆单元测试.
  2. 创建模拟,输入变量等初始化的各种不同排列/组合.
  3. 根据一组基于某些参数的此类初始化,使用来运行每个给定的单元测试.

我将如何做这样的事情?

How would i go about doing something like this?

是否已有任何框架可以处理此问题(即在更改初始化时多次运行给定的测试)?你能建议我可以用来做些什么的任何设计或想法吗?

Is there already any framework to handle this (I.e. run a given test multiple times while changing initialization)? Can you suggest any design or ideas with which i could make something to do this?

我知道单元测试框架的工作.我自己使用NUnit和Rhino模拟.

I am aware of unit testing frame works. i use NUnit and Rhino mocks myself.

下面显示的是我需要的示例.

Shown below is an example of what i need.

[Test Initialize]
Setup( <-possible parameter-> )

[Test Method]
TestA()

现在我希望TestA()可以多次运行.每次测试初始化​​都会选择另一个初始化组合.

now i want TestA() to be run multiple times. Each time the Test initialize would pick another initialization combination.

更多说明

More clarification

让我们假设一个测试需要变量A,B,C.它们每个都是非常复杂的对象,最终结果是可以形成大量的组合.因此,我希望以某种方式可以创建一个测试初始化​​,从而可以遍历此类组合的列表,以便对其进行初始化,运行TESTA,返回列表中的下一个初始化,再次运行TESTA,依此类推,直到列表用完了.接下来,它为TESTB选择另一个列表,然后再次执行此过程.

Lets suppose a test would require variables A, B, C. Each of them are very complex objects with the end result that the a large number of combinations can be formed. So i'm hoping that somehow i could create a test initialize that could possible iterate through a list of such combinations, so it would initialize them, run the TESTA, go back to next initialization in the list, run TESTA again and so on until the list runs out. Next it picks another list for TESTB and once again follows this process.

至少我希望能够运行n次给定TEST函数的能力.剩下的我知道我可以在可能的情况下建造

At the least im hoping for some ability to be able to run a given TEST function n times. The rest i know i can build once this is possible

推荐答案

在nUnit中,您可以将[TestCase]属性用于简单类型:

In nUnit you can use the [TestCase] attribute for simple types:

[Test]
[TestCase("a", "b")]
[TestCase("c", "b")]
[TestCase("a", "d")]
public void TestMethod(string param1, string param2){
   // run your test with those parameters
}

或者您可以将 TestCaseSource 方法用于复杂类型:

Or you can use a TestCaseSource method for complex types:

[Test]
[TestCaseSource("GetTestCases")]
public void TestMethod(MyObject1 param1, MyObject2 param2){
   // run your test with those parameters
}

private IEnumerable GetTestCases(){
   yield return new TestCaseData( new MyObject1("first test args"), 
                                  new MyObject2("first test args"))
                        .SetName("SomeMeaningfulNameForThisTestCase" );
   yield return new TestCaseData( new MyObject1("2nd test args"), 
                                  new MyObject2("2nd test args"))
                        .SetName("SomeMeaningfulNameForThisTestCase2" );

}

您可以使用数据源在MS-Test中执行类似的操作: http://codeclimber .net.nz/archive/2008/01/18/How-to-simulate-RowTest-with-MS-Test.aspx

You can do something similar in MS-Test using a DataSource: http://codeclimber.net.nz/archive/2008/01/18/How-to-simulate-RowTest-with-MS-Test.aspx

这篇关于Visual Studio C#单元测试-使用不同/多个测试初始化​​运行单元测试,多次运行同一单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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