c# - 如何使用 C#、Visual Studio MS Test 获取测试用例的 TestCategory

标签 c# mstest categories testcaseattribute

我希望使用 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");

获取方法基并获取属性,如上所述。

来源:Read the value of an attribute of a method

关于c# - 如何使用 C#、Visual Studio MS Test 获取测试用例的 TestCategory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33045103/

相关文章:

c# - 没有@的编译错误

c# - 以编程方式创建 MSTest 单元测试

c# - 回发后如何检查表单

c# - 银光反射

c# - 如何在单元测试中模拟/伪造 SmtpClient?

python - 如何用 True、False 和颜色编码 Python 绘图来标记 Y 轴?

Objective-C 类别

css - Opencart 类别模块中每个 UL 的不同样式

c# - 如何获取字符串的瑞典语排序顺序

c# - 缺少[程序集 :RuntimePlugin] attribute in SpecFlowPlugin