我結束了使用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'。 –