【问题标题】:Set TestCategory attribute for unit tests为单元测试设置 TestCategory 属性
【发布时间】:2022-01-23 14:03:37
【问题描述】:

我正在从某个数据源读取测试集合。所以我得到了一个对象集合,包括:每个对象的 testAssembly、testClass、testName 和 groupID。 我正在尝试为每个 testName 创建 Testcategory 属性并给他 groupID 的方法。 evrey 测试可以在不同的程序集、解决方案或类中 谢谢

示例: 之前:

[TestMethod]
public void Test_test()
{
  code....
}

之后:

[TestCategory(groupID)]
[TestMethod]
public void Test_test()
{
  code....
}

【问题讨论】:

    标签: c# visual-studio testing


    【解决方案1】:

    您可以编写自定义属性。有a lot of material for this online

    示例(来自链接):

    [System.AttributeUsage(System.AttributeTargets.Class |  
                       System.AttributeTargets.Struct)  
    ]  
    public class AuthorAttribute : System.Attribute  
    {  
       private string name;  
       public double version;  
    
       public AuthorAttribute(string name)  
       {  
            this.name = name;  
            version = 1.0;  
       }  
    }  
    

    用法:

    [Author("P. Ackerman", version = 1.1)]  
    class SampleClass  
    {  
        // P. Ackerman's code goes here...  
    }  
    

    【讨论】:

    • 我的问题不是如何写一个属性。我问的是我如何在测试列表上运行,当每个测试可以位于不同的命名空间或类程序集中时,每个人都添加属性
    • @דודושטראוס 哦,对不起,要么问题不够清楚,要么我误解了。无论哪种方式...您是否知道部分类的概念。这可能是您问题的答案。
    • 我不确定部分类如何帮助我我需要经历一些层... 1)如何从我的代码中读取 dll(dll 名称 = testAssembly。2)找到dll 中的特定类(类名 = testClass)。 3)在类中找到spsific方法(method = testName)。 4) 为方法添加一个属性。
    猜你喜欢
    • 2011-04-03
    • 2014-08-16
    • 1970-01-01
    • 2013-03-03
    • 2020-08-02
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2017-10-30
    相关资源
    最近更新 更多