【问题标题】:xUnit.net: Test class constructors not being run?xUnit.net:未运行测试类构造函数?
【发布时间】:2016-06-18 18:48:05
【问题描述】:

我正在尝试在我的 xUnit.net 测试类之一中运行一些设置代码,但尽管测试正在运行,但构造函数似乎并不存在。

这是我的一些代码:

public abstract class LeaseTests<T>
{
    private static readonly object s_lock = new object();
    private static IEnumerable<T> s_sampleValues = Array.Empty<T>();

    private static void AssignToSampleValues(Func<IEnumerable<T>, IEnumerable<T>> func)
    {
        lock (s_lock)
        {
            s_sampleValues = func(s_sampleValues);
        }
    }

    public LeaseTests()
    {
        AssignToSampleValues(s => s.Concat(CreateSampleValues()));
    }

    public static IEnumerable<object[]> SampleValues()
    {
        foreach (T value in s_sampleValues)
        {
            yield return new object[] { value };
        }
    }

    protected abstract IEnumerable<T> CreateSampleValues();
}

// Specialize the test class for different types
public class IntLeaseTests : LeaseTests<int>
{
    protected override IEnumerable<int> CreateSampleValues()
    {
        yield return 3;
        yield return 0;
        yield return int.MaxValue;
        yield return int.MinValue;
    }
}

我将SampleValues 用作MemberData,所以我可以在这样的测试中使用它们

[Theory]
[MemberData(nameof(SampleValues))]
public void ItemShouldBeSameAsPassedInFromConstructor(T value)
{
    var lease = CreateLease(value);
    Assert.Equal(value, lease.Item);
}

但是,对于所有使用 SampleValues 的方法,我一直收到一条错误消息,提示“没有为 [method] 找到数据”。在我进一步调查之后,我发现 LeaseTests 构造函数甚至没有运行;当我在对AssignToSampleValues 的调用上设置断点时,它没有被命中。

为什么会发生这种情况,我可以做些什么来解决它?谢谢。

【问题讨论】:

    标签: c# .net xunit xunit.net


    【解决方案1】:

    构造函数没有运行,因为在创建特定测试类的实例之前评估了MemberData。我不确定这是否能满足您的要求,但您可以执行以下操作:

    定义ISampleDataProvider接口

    public interface ISampleDataProvider<T>
    {
        IEnumerable<int> CreateSampleValues();
    }
    

    添加类型特定的实现:

    public class IntSampleDataProvider : ISampleDataProvider<int>
    {
        public IEnumerable<int> CreateSampleValues()
        {
            yield return 3;
            yield return 0;
            yield return int.MaxValue;
            yield return int.MinValue;
        }
    }
    

    SampleValues方法中解析和使用数据提供者

    public abstract class LeaseTests<T>
    {
        public static IEnumerable<object[]> SampleValues()
        {
            var targetType = typeof (ISampleDataProvider<>).MakeGenericType(typeof (T));
    
            var sampleDataProviderType = Assembly.GetAssembly(typeof (ISampleDataProvider<>))
                                                    .GetTypes()
                                                    .FirstOrDefault(t => t.IsClass && targetType.IsAssignableFrom(t));
    
            if (sampleDataProviderType == null)
            {
                throw new NotSupportedException();
            }
    
            var sampleDataProvider = (ISampleDataProvider<T>)Activator.CreateInstance(sampleDataProviderType);
            return sampleDataProvider.CreateSampleValues().Select(value => new object[] {value});
        }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多