如何在 MSTest 的测试执行期间添加非测试方法作为测试?
How can I add a non-test-method as test during test execution in MSTest?
我有以下设置 运行 我的并行测试(对于 Selenium 测试):
[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);;
}
}
}
虽然我的 Jenkins 服务器上的测试结果报告只显示一个测试有 运行,但它工作正常。我想在报告中看到 运行 由 TestAllParallel()
完成的所有测试。
这可以吗?我在想也许可以在 运行 时间内添加一个方法作为对 MSTest 的测试。
恐怕无法添加方法,但您可以报告该特定测试中所有运行的结果。首先用类似下面的代码包装测试方法的执行,这样你就可以保存运行结果:
private static void ExecuteMethod(MethodInfo method, object instance)
{
try
{
method.Invoke(instance, null);
threadSafeStringBuilder.Append("Test: " + method.Name + " passed");
}
catch (UnitTestAssertException utException)
{
threadSafeStringBuilder.Append("Test: " + method.Name + " assertion failed" + utException.Message);
_allPassed = false;
}
catch (Exception ex)
{
threadSafeStringBuilder.Append("Test: " + method.Name + " exception: " + ex.Message");
}
}
稍后在 testAllParallel 中:
public void TestAllParallel()
{
//...
System.Threading.Tasks.Parallel.Invoke(testActions);
if(_allPassed){
Assert.IsTrue(true, "All tests Passed:\n"+threadSafeStringBuilder.ToString());
}else{
Assert.Fail("Some tests failed:\n"+threadSafeStringBuilder.ToString());
}
)
请记住,这只是一个想法,并非完全有效的代码。特别是 .net 中没有 ThreadSafeStringBuilder
,所以您可能需要自己实现或使用一些库。
使用 System.Diagnostics.WriteLine
的另一个系统可能同样有效。它将输出您在其中写入的任何内容。这是讨论它的另一个 SO 问题:Add custom message to unit test result
该线程中一个非常有趣的点是,如果没有状态消息,有人可以通过删除所有测试内容来 "sabotage" 您的测试以使其通过,因此您可能需要在正常测试中发送诊断信息.
我不确定我是否正确理解了你的问题,但我认为你应该稍微改变你的解决方案以克服这个挫折。
尝试在 Jenkins 中构建一个只执行命令行的项目,该命令可以是使用 MSTest 命令执行所有测试。
你可以看看怎么做 here
我有以下设置 运行 我的并行测试(对于 Selenium 测试):
[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);;
}
}
}
虽然我的 Jenkins 服务器上的测试结果报告只显示一个测试有 运行,但它工作正常。我想在报告中看到 运行 由 TestAllParallel()
完成的所有测试。
这可以吗?我在想也许可以在 运行 时间内添加一个方法作为对 MSTest 的测试。
恐怕无法添加方法,但您可以报告该特定测试中所有运行的结果。首先用类似下面的代码包装测试方法的执行,这样你就可以保存运行结果:
private static void ExecuteMethod(MethodInfo method, object instance)
{
try
{
method.Invoke(instance, null);
threadSafeStringBuilder.Append("Test: " + method.Name + " passed");
}
catch (UnitTestAssertException utException)
{
threadSafeStringBuilder.Append("Test: " + method.Name + " assertion failed" + utException.Message);
_allPassed = false;
}
catch (Exception ex)
{
threadSafeStringBuilder.Append("Test: " + method.Name + " exception: " + ex.Message");
}
}
稍后在 testAllParallel 中:
public void TestAllParallel()
{
//...
System.Threading.Tasks.Parallel.Invoke(testActions);
if(_allPassed){
Assert.IsTrue(true, "All tests Passed:\n"+threadSafeStringBuilder.ToString());
}else{
Assert.Fail("Some tests failed:\n"+threadSafeStringBuilder.ToString());
}
)
请记住,这只是一个想法,并非完全有效的代码。特别是 .net 中没有 ThreadSafeStringBuilder
,所以您可能需要自己实现或使用一些库。
使用 System.Diagnostics.WriteLine
的另一个系统可能同样有效。它将输出您在其中写入的任何内容。这是讨论它的另一个 SO 问题:Add custom message to unit test result
该线程中一个非常有趣的点是,如果没有状态消息,有人可以通过删除所有测试内容来 "sabotage" 您的测试以使其通过,因此您可能需要在正常测试中发送诊断信息.
我不确定我是否正确理解了你的问题,但我认为你应该稍微改变你的解决方案以克服这个挫折。 尝试在 Jenkins 中构建一个只执行命令行的项目,该命令可以是使用 MSTest 命令执行所有测试。 你可以看看怎么做 here