在MSTest中,如何查找要在ClassInitialize或AssemblyInitialize中运行的测试总数

本文关键字:AssemblyInitialize 运行 测试 ClassInitialize MSTest 何查找 查找 | 更新日期: 2023-09-27 18:23:37

使用MStest-我想找到排队等待运行的测试方法的总数。

我应该如何在ClassInitialize()AssemblyInitialize()方法中捕获此值。

我唯一得到的是TestContext,它并没有测试总数的详细信息。

在MSTest中,如何查找要在ClassInitialize或AssemblyInitialize中运行的测试总数

我确实有一个答案,但你必须使用Reflection。这个方法获取类中的所有测试并运行它们。每个测试都以测试用例的字符串"TC"开始-苏鲁

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;
        }
    }

经过无数次尝试,我一直在使用以下方法来实现这一点。

/// <summary>
/// Gets the number of test methods in the specified class.
/// </summary>
/// <param name="testClass">The Type object representing the class to be checked.</param>
/// <returns>The number of test methods in the class.</returns>
public int GetNumberOfTestMethods(Type testClass)
{
    var withTestAttribute = testClass.GetMethods()
        .Where(m => m.GetCustomAttributes(typeof(TestMethodAttribute), false).Any());
    return withTestAttribute.Count();
}

您可以在SampleTest类中这样调用此方法。

var count = GetNumberOfTestMethods(typeof(SampleTest))

为了实现这一点,您的测试方法应该具有[TestMethod]属性。

或者(如果你想对过滤器有更多的控制权)你也可以这样做。但上述解决方案更优化,因为它并没有过滤器。

public int GetNumberOfTestMethods(Type testClass)
{
    var attributeData = testClass.GetMethods()
        .SelectMany(x => x.CustomAttributes)
        .Where(y => y.AttributeType == typeof(TestMethodAttribute)).ToList();
    return attributeData.Count;
}
相关文章:
  • 没有找到相关文章