如何获取已调用 TestMethod 的 TestCategory 名称?

How to fetch TestCategory name for which the TestMethod has been invoked?

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

如果一个 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?

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

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

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