4

I have a large suite of test cases. I want to run all the tests in the testfixtures, one at a time. Running them all in one batch in NUnit does not do what I want.

To do this, I want to get all the test cases' names in a list and loop through them. Any pointers?

TheHans255
  • 2,059
  • 1
  • 19
  • 36
pink
  • 51
  • 3
  • 1
    Why just not run all them in one batch using NUnit console or ReSharper tests runner? – sll Jan 27 '12 at 13:40
  • No, I need to do some execution after each test case. Cant run them in one batch. – pink Jan 27 '12 at 13:40
  • Also, I would not like to use any categories, since I have a large suite and it wont be possible to add a category to each test case. – pink Jan 27 '12 at 13:41
  • 1
    Which executiion you need? Can you code it using C#? If so just put in `TearDown` stage so it would be executed automatically after the each test in this `TestFixture` – sll Jan 27 '12 at 13:43
  • I need to run a coverage tool to find out the method that this Test Case hit. Thats a good idea But as I told you, my test suite is already in place, and I dont want to add any code to the testfixtures. So, I cant really add anything to the TearDown. Instead, I will have another class in which I plan to iterate through each test case, run it and then find its coverage details using NCover. – pink Jan 27 '12 at 13:46
  • 1
    Why you need that? Generally tests should ensure that some logic behave as expected, why you need to see which methods are affetced? Why just not run `NCover.Console.exe //x Coverage.xml nunit-console.exe Assembly1.Test.dll Assembly2.Test.dll` from command line and get entire covergae report? – sll Jan 27 '12 at 13:49
  • No, that will give me the coverage report for all the tests in the fixture. I need the report per test case – pink Jan 27 '12 at 13:51
  • You can spend hours fighting the tools, or you can use them as designed (as @sll suggests). – TrueWill Jan 27 '12 at 13:58
  • From code covergae ideology perspectives this does not make sense since you should be interested in Coverage per class/method, not opposite. IIRC Visual Studio test suit [able to show](http://msdn.microsoft.com/en-us/vstudio/ff420671.aspx) which methods covered by a particular MSTest not NUnit test but I'm not sure – sll Jan 27 '12 at 13:59
  • See this post [.NET - determining which test cases covered a method](http://stackoverflow.com/questions/8416412/net-determining-which-test-cases-covered-a-method) – sll Jan 27 '12 at 14:02
  • Note that there are other usages for this besides test coverage. I came here wanting to split the tests in a test suite between multiple processes (doing some kind of divide and conquer on the test names). – TheHans255 Feb 14 '19 at 19:12

2 Answers2

3

You could try reflecting the assembly and pulling out all the methods that have the [Test] attribute:

List<MethodInfo> testMethods = new List<MethodInfo>();
Assembly x = Assembly.LoadFile("CompiledTests");
Type[] classes = x.GetExportedTypes();
foreach (Type type in classes)
{
    MethodInfo[] methods = type.GetMethods();
    foreach (MethodInfo methodInfo in methods)
    {
        if (methodInfo.GetCustomAttributes(typeof(TestAttribute), true).Length == 1)
        {
            testMethods.Add(methodInfo);
        }
    }
}
breed052
  • 195
  • 1
  • 2
  • 11
0

Another alternative would be to use the implementation used by NUnit VS Adapter: https://github.com/nunit/nunit-vs-adapter/blob/master/src/NUnitTestAdapter/NUnitTestDiscoverer.cs

Basically you can use built in NUnit logic to find out what Test Cases there are. The sample code above is from NUnit 2, but I think something similar can work for NUnit 3 as well (otherwise, how would a test adapter find the test cases ...

ohnezahn
  • 453
  • 4
  • 9