我结束了使用NUnit的SuiteAttribute。
这种方法涉及到创建“伞类”,比如:
namespace Validator {
public class AllTests {
[Suite]
public static IEnumerable Suite {
get {
var directory = @"[ImplementationAssembliesPath]";
var suite = new ArrayList();
// GetInstances is a method responsible for loading the
// assemblys and instantiating the implementations to be tested.
foreach (var instance in GetInstances(directory)) {
suite.Add(GetResolvedTest(instance));
}
return suite;
}
}
// This part is crucial - this is where I get to inject the
// implementations to the test.
private static Object GetResolvedTest(ICalculator instance) {
return new CalculatorTests {Calculator = instance};
}
[...]
}
注意,测试类有注入我想实现的属性。我选择属性注入是因为测试运行者通常不喜欢默认的构造函数。但是,我不得不从实际测试类中删除TestFixtureAttribute
(这里省略),以免混淆控制台运行程序的运行。
然后我创建了一个简单的控制台应用程序与/fixture
参数运行NUnit的控制台 - 亚军:
namespace TestRunner {
using System;
using NUnit.ConsoleRunner;
internal class Program {
private static void Main(String[] args) {
var testDllPath = @"[TestAssemblyPath]/Validator.dll";
var processArgument = @"/process=Separate";
var domainArgument = @"/domain=Multiple";
var runtimeArgument = @"/framework=4.5";
var shadowArgument = @"/noshadow";
var fixtureArgument = String.Format(@"/fixture={0}", "[Namespace].AllTests");
Runner.Main(new[] {
testDllPath,
processArgument,
domainArgument,
runtimeArgument,
shadowArgument,
fixtureArgument
});
Console.ReadLine();
}
}
}
我仍然有兴趣听到这个您的意见,并在替代解决方案。
您可能想考虑使用'TestCaseSource'属性来提供您想要测试的类的类型/实例。你需要加载程序集等,但是NUnit会为你处理其余的管道工作。 http://www.nunit.org/index.php?p=testCaseSource&r=2.5.9 – forsvarir
'TestCaseSource'是一个选项,我同意,但是不会达到_running每个测试的目标 - 一个实现,然后继续下一个实现,重复process_。由于没有得到一个很好的解决方案来实现'IAddin'和'ISuiteBuilder',我目前正在研究看起来很有前途的'SuiteAttribute'。 –