I am using Selenium with C# for Automation, and I want to invoke NUnit through code as follows:
CoreExtensions.Host.InitializeService();
TestPackage testPack
This sample code should give you an idea of what you have to do to start cycling through your tests and selecting which one you want to run. I have used several array indexes of 0 where you should loop through instead.
The Jist of it is that you have to actually load the tests before you can start trying to run them individually as the tests need to have a unique TestId which only gets set once they have been loaded. The following code works and runs the first test in the first testfixture in your testing framework. It also scans all the test names for you to either display or run based on some criteria
CoreExtensions.Host.InitializeService();
TestSuiteBuilder builder = new TestSuiteBuilder();
TestPackage testPackage = new TestPackage(@"path.to.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
TestSuite suite = builder.Build(testPackage);
TestSuite test = suite.Tests[0] as TestSuite;
var numberOfTests = ((TestFixture)test.Tests[0]).TestCount;
foreach (TestMethod t in ((TestFixture)test.Tests[0]).Tests)
{
Console.WriteLine(t.TestName.Name);
}
TestName testName = ((TestMethod)((TestFixture)test.Tests[0]).Tests[0]).TestName;
TestFilter filter = new NameFilter(testName);
TestResult result = test.Run(new NullListener(), filter);
ResultSummarizer summ = new ResultSummarizer(result);
Assert.AreEqual(1, summ.ResultCount);