xunit:在ClassData的类中获取测试方法名称

问题描述 投票:0回答:1

这是我的样本测试,

    [Theory]
    [BeforeAfterMethod]
    [ClassData(typeof(GetTestData))]
    public void Test3(int a)
    {
        ReporterLogs.Pass("value of a: " + a);
        //TODO
    }

我的GetTestData类是,

 public class GetTestData : IEnumerable<object[]>
  {
    private string _currentTestMethodName;
    private List<object[]> _data = new List<object[]>();

    public GetTestData()
    {
        Console.WriteLine(**Print test method name here**);
        _data.Add(new object[] { 10 } });
    }

    public IEnumerator<object[]> GetEnumerator()
    {
        return _data.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

}

我想在GetTestData类中打印当前测试(在本例中为Test3)方法名称。如何实现。提前致谢。

c# xunit xunit.net
1个回答
0
投票

它可以通过在Xunit中使用DataAttribute来实现,如下所示,

public class GetTestData : DataAttribute
{
    private List<object[]> _data = new List<object[]>();

    public override IEnumerable<object[]> GetData(MethodInfo testMethod)
    {
        if (testMethod == null)
              throw new ArgumentNullException(testMethod.Name);

        _data.Add(new object[] { 10 });
        return _data;
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.