Reputation: 1315
I'll start with an problem. I have a little library. It goes through a list of urls in text files and create assertions depending on the url returns 200 or not.
The goal is to make a test runner like Reshaprer, Testdriven.net or a CI server like team city or hudson to pick up the assertions as tests.
My question is how do I extend/build upon xunit or nunit to run tests from another source? Examples would be great.
Update To make things a bit more clear. I should be able to look load the dll in the test runner that ships with xunit/nunit and run the test.
How the list of urls is populated is very dynamic. Depending on certain things more url can be created. So a list of data is not an option.
Upvotes: 1
Views: 969
Reputation: 25732
With xUnit, all you need is to implement ITestRunner
interface, i.e.:
public class MyTestRunner : ITestRunner
{
// Methods
public MyTestRunner();
public static TestRunState RunAssembly(TestRunner runner);
public static TestRunState RunClass(TestRunner runner, Type type);
public static TestRunState RunClassWithInnerTypes(TestRunner runner, Type type);
public static TestRunState RunMethod(TestRunner runner, MethodInfo method);
TestRunState ITestRunner.RunAssembly(ITestListener listener, Assembly assembly);
TestRunState ITestRunner.RunMember(ITestListener listener, Assembly assembly, MemberInfo member);
TestRunState ITestRunner.RunNamespace(ITestListener listener, Assembly assembly, string ns);
}
For implementation details, grab the source code of xUnit and have a look at the sample runners.
Upvotes: 1
Reputation: 56457
You can use the TestCaseSourceAttribute
in NUnit to run dynamic tests.
Upvotes: 0
Reputation: 54130
If you mean how can you run NUnit tests programmatically (instead of using one of the supplied NUnit runners), then it's actually pretty easy:
using System;
using NUnit.Core;
using NUnit.Framework;
using MyProject.Tests; /* assuming MyTestFixture is defined in this project/assembly */
namespace TestRunner.DemoApp {
class Program {
static void Main(string[] args) {
var builder = new TestSuiteBuilder();
var testAssemblyLocation = typeof(MyTestFixture).Assembly.Location;
var package = new TestPackage(testAssemblyLocation);
var suite = builder.Build(package);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Running tests from " + testAssemblyLocation;
Console.WriteLine("Testing against " + ConfigurationManager.AppSettings["HostNameSuffix"]);
var result = suite.Run(new NullListener(), TestFilter.Empty);
switch (result.ResultState) {
case ResultState.Success:
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Pass.");
break;
case ResultState.Error:
case ResultState.Failure:
Console.ForegroundColor = ConsoleColor.Red;
DrawResults(result.Results, r => r.ResultState == ResultState.Error || r.ResultState == ResultState.Failure);
break;
}
static void DrawResults(IList results, Func<TestResult, bool> include) {
foreach (var obj in results) {
var result = obj as TestResult;
if (result == null) continue;
if (result.Results != null && result.Results.Count > 0) {
DrawResults(result.Results, include);
} else if (include(result)) {
Console.WriteLine(result.Name);
Console.WriteLine(result.Message);
Console.WriteLine(result.ResultState);
Console.WriteLine(result.StackTrace);
Console.WriteLine(String.Empty.PadLeft(Console.WindowWidth, '='));
}
}
}
}
}
Upvotes: 1