Visual Studio C# unit testing - Run Unit test with

2019-03-27 10:34发布

问题:

What i want to do is this :

  1. Create a bunch of Unit Tests.
  2. Create a variety of different permutations/combinations of initialization of mocks, input variables etc.
  3. Run each given unit test with a against a set of such initializations based on some parameters.

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?

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()

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

More clarification

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.

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

回答1:

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
}

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" );

}

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



回答2:

You might be able to do this without needing any framework-specific addons by creating an abstract base class that contains all your test functions, then inheriting that base class with multiple classes, each with their own setup function.

public abstract class MyTests
{
    [Test]
    public void TestOne()
    {
        ...
    }

    [Test]
    public void TestTwo()
    {
        ...
    }
}
[TestFixture]
public class FirstSetup : MyTests
{
    [Setup]
    public void Setup()
    {
        ...
    }
}

[TestFixture]
public class SecondSetup : MyTests
{
    [Setup]
    public void Setup()
    {
        ...
    }
}

I have done this in other languages, but not sure how the various C# frameworks will handle it.