我有以下設置並聯運行我的測試(用於Selenium測試):如何在MSTest的測試執行過程中添加一個非測試方法作爲測試?
[TestClass]
public class ParallelTests
{
[TestMethod]
public void TestAllParallel()
{
var testActions = Assembly.GetExecutingAssembly().GetTypes()
.Where(t =>
t.GetCustomAttribute<CompilerGeneratedAttribute>() == null &&
t.GetCustomAttribute<TestClassAttribute>() != null)
.Select(t => new Action(() => RunTest(t)))
.ToArray();
System.Threading.Tasks.Parallel.Invoke(testActions);
}
private static void RunTest(Type test)
{
var instance = Activator.CreateInstance(test);
ExecuteTestMethods(instance);
}
private static void ExecuteTestMethods(object instance)
{
var testMethods = instance.GetType().GetMethods()
.Where(m =>
m.GetCustomAttribute<TestMethodAttribute>(false) != null &&
m.GetCustomAttribute<IgnoreAttribute>(false) == null)
.ToArray();
foreach (var methodInfo in testMethods)
{
methodInfo.Invoke(instance, null);;
}
}
}
這工作得很好,雖然我的詹金斯服務器上的測試結果報告只能說明一個測試已運行。我希望在報告中看到由TestAllParallel()
運行的所有測試。
這可能嗎?我在想,也許可以在運行時將方法作爲測試添加到MSTest中。
我不認爲mstest有一個方法來在運行時添加測試。您可以爲測試創建一個單獨的trx文件(在您的方法中生成文件),然後Jenkins中的mstest插件會認爲它是從MSTest輸出的。 –
也許你可以嘗試檢查測試代碼本身的代碼覆蓋率(而不是被測試的代碼)。雖然我不知道如何在jenkins和mstest中做到這一點。 –
爲什麼你自己執行所有的測試,而不是指導你的Jenkins服務器爲你做? – silver