【问题标题】:How to get Author Attribute when it's set for TestFixture level instead of Test level?当设置为 TestFixture 级别而不是测试级别时如何获取作者属性?
【发布时间】:2019-04-29 23:22:24
【问题描述】:

我想为 testFixture 设置一个作者并在运行时获取它以便能够在我的数据库中获取它:

这是TestFixture 类 我为 testFixture 级别设置了 Nunit 属性作者:

[TestFixture(Author = nameof(AuthorName.Eva)),MachineCategory.Filter]
public class FilterTests : WidiaTestSetup
{  

        [Test, RetryOnFailureOrException]
        public void FilterCuttingDiameter()
        { ...}
} 

要从 test 级别获取测试作者,我会这样做:

Author = TestContext.CurrentContext.Test.Properties.Get("Author").ToString()

但它在 testFixture 中不起作用 如何从TestFixture 级别获取它?

提前致谢

【问题讨论】:

    标签: c# unit-testing testing nunit


    【解决方案1】:

    它在灯具的TestContext 中可用。只需将其保存在 OneTimeSetUp 方法中的某个位置即可。

    [OneTimeSetUp]
    public void GrabTheAuthor()
    {
        _fixtureAuthor = TestContext.CurrentContext.Test.Properties.Get("Author").ToString();
    }
    

    【讨论】:

      【解决方案2】:

      虽然它不是一个非常优雅的解决方案,但反射可能是您最好的选择。

      var author = GetType().CustomAttributes.First().NamedArguments.First(x => x.MemberName.Equals("Author")).TypedValue.ToString();
      

      我建议在您的 OneTimeSetup 中执行此操作,以避免每次测试产生任何不必要的成本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多