本文介绍了在MSTest中,如何找到要在ClassInitialize或AssemblyInitialize中运行的测试总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用MStest-我想查找排队等待运行的测试方法的总数.
Using MStest - I want to find the total number of test methods which are queued for run.
如何在ClassInitialize()
或AssemblyInitialize()
方法中捕获此值.
How should I capture this value in ClassInitialize()
or AssemblyInitialize()
method.
我只能得到的是TestContext,它没有测试总数的详细信息.
Only thing I get is TestContext which has no details of total number of tests.
推荐答案
我实际上对此有一个答案,但是您必须使用Reflection来做.此方法获取该类中的所有测试并运行它们.每个测试以测试用例的字符串"TC"开头. -sulu
I actually have an answer to this one, but you have to use Reflection to do it. This method gets all of the tests in the class and runs them. Each test starts with the string "TC" for test case. -sulu
public async Task RunServiceTests()
{
// Find the list of Test Cases in the running assembly
// all methods that start with "TC"
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] types = assembly.GetExportedTypes();
string testCaseString = "TC";
Dictionary<long, MethodInfo> dictionary = new Dictionary<long, MethodInfo>();
long testCaseNumber = 0;
MethodInfo[] methods = null;
foreach (Type t in types)
{ if(t.Name == "ServicesTests")
{
methods = t.GetMethods();
foreach (MethodInfo method in methods)
{
Regex regex = new Regex(@"TC\d+");
Match match = regex.Match(m.Name);
if (match.Success)
{
simpleLogger.WriteLine("Method Name: " + method.Name);
int pos = -1;
string name = method.Name;
pos = name.IndexOf("_");
int length = pos - 2;
testCaseString = name.Substring(2, length);
simpleLogger.LogInfo("Test Case Number found: " + testCaseString);
testCaseNumber = Convert.ToInt64(testCaseString);
if (!dictionary.ContainsKey(testCaseNumber))
{
dictionary.Add(testCaseNumber, method);
}
}
} // End of methodInfo loop
break;
}
}
foreach (KeyValuePair<long, MethodInfo> kvp in dictionary)
{
MethodInfo method = kvp.Value;
Task task = (Task) method.Invoke(Instance, null);
await task;
}
}
这篇关于在MSTest中,如何找到要在ClassInitialize或AssemblyInitialize中运行的测试总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!