当为 TestFixture 级别而不是测试级别设置作者属性时如何获取它?

How to get Author Attribute when it's set for TestFixture level instead of Test level?

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

这里是 TestFixture class 我为 testFixture 级别设置了 Nunit 属性 Author :

[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 级别获得它?

提前致谢

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

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

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

它在夹具的 TestContext 中可用。只需将其保存在 OneTimeSetUp 方法中的某处即可。

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