【问题标题】:How to fetch TestCategory name for which the TestMethod has been invoked?如何获取已调用 TestMethod 的 TestCategory 名称?
【发布时间】:2018-03-12 10:31:56
【问题描述】:

我正在尝试获取 MSTest 中已运行 TestMethod 的特定 TestMethod 的 TestCategory 名称。

如果一个 TestMethod 有多个 TestCategory,该方法是针对哪个特定的 TestCategory 调用的?

我正在使用下面的代码获取该方法的所有 TestCategories,但如何找到调用该方法的特定类别。

[TestMethod, TestCategory("Reg"), TestCategory("Smoke"), TestCategory("Sanity"), TestCategory("PathTest")]
public void TestMethod_testCatetory()
{
       var method = MethodBase.GetCurrentMethod();
        foreach (var attribute in (IEnumerable<TestCategoryAttribute>)method
                    .GetCustomAttributes(typeof(TestCategoryAttribute), true))
          {
                foreach (var category in attribute.TestCategories)
                {
                     Console.WriteLine(category);
                }
         }
} 

从 cmd 将使用以下命令调用它

C:\VSTest.Console.exe LocationOfDLL /TestCaseFilter:"TestCategory=Smoke" 

是否可以获取用于调用方法的 TestCategory is Smoke?

【问题讨论】:

    标签: c# mstest azure-pipelines


    【解决方案1】:

    您无法直接在测试方法代码中获取当前类别。 您可以从 RunSettings 文件 (TestContext.Properties[xxx]) 中读取值并在测试期间覆盖参数,简单步骤:

    1. 定义一个变量来存储类别
    2. 在 Visual Studio 测试任务的测试筛选条件输入框中使用该变量指定筛选器
    3. 在 Visual Studio 测试任务的 Override TestRun 参数输入框中指定参数

    有一个博客可以帮助你:Supplying Run Time Parameters to Tests

    【讨论】:

    • 对于其他寻找答案的人,仅当测试是从构建和发布而不是从测试资源管理器或 cmd 运行时才有效。在 cmd 中没有覆盖 TestRun 参数的选项。
    • @Vincent 是的,不申请测试浏览器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2011-07-03
    • 2014-07-11
    • 2017-08-22
    • 2013-07-07
    • 2011-10-27
    • 2021-04-16
    相关资源
    最近更新 更多