我希望使用 C# 在运行时查找测试用例的测试类别。我正在使用 MSTEST,TestContext 没有任何与 TestCategory 相关的信息,我想捕获/记录 TestCategory 信息。就我而言,我有多个 TestCATEGORIES 分配给一个测试用例。示例
BaseTest 将具有初始化和 CleanUp 方法..
[TestClass]
public class CustomerTest : BaseTest
{
[TestMethod]
[TestCategory("Smoke")]
[TestCategory("regression")]
public void Login()
最佳答案
您可以使用反射来获取测试方法中的属性,如下所示:
[TestMethod]
[TestCategory("Smoke")]
[TestCategory("regression")]
public void Login()
{
var method = MethodBase.GetCurrentMethod();
foreach(var attribute in (IEnumerable<TestCategoryAttribute>)method
.GetCustomAttributes(typeof(TestCategoryAttribute), true))
{
foreach(var category in attribute.TestCategories)
{
Console.WriteLine(category);
}
}
var categories = attribute.TestCategories;
}
如果您想在测试方法之外的其他位置获取类别,您可以使用
var method = typeof(TheTestClass).GetMethod("Login");
获取方法基并获取属性,如上所述。
关于c# - 如何使用 C#、Visual Studio MS Test 获取测试用例的 TestCategory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33045103/