I have a nunit class library containing test cases. I want to programmatically get a list of all tests in the library, mainly the test names and their test ids. Here is what I have so far:
var runner = new NUnit.Core.RemoteTestRunner();
runner.Load(new NUnit.Core.TestPackage(Request.PhysicalApplicationPath + "bin\\SystemTest.dll"));
var tests = new List<NUnit.Core.TestResult>();
foreach (NUnit.Core.TestResult result in runner.TestResult.Results)
{
tests.Add(result);
}
The issue is that runner.TestResult is null until you actually run the tests. I obviously don’t want to run the tests at this point, I just want to get a list of which tests are in the library. After that, I will give users the ability to select a test and run it individually, passing in the test id to the RemoteTestRunner instance.
So how can I get the list of tests without actually running all of them?
You could use reflection to load the assembly and look for all the test attributes. This would give you all the methods that are test methods. The rest is up to you.
Here is an example on msdn about using reflection to get the attributes for a type.
http://msdn.microsoft.com/en-us/library/z919e8tw.aspx