gpt4 book ai didi

c# - XUnit CollectionFixture 实例化多次

转载 作者:行者123 更新时间:2023-12-05 01:39:33 25 4
gpt4 key购买 nike

我才刚刚开始使用 ClassFixtures 和收藏 xUnit所以我可能做错了,但我的理解是我可以创建一个 ICollectionFixture<MyFixture> ,通过 CollectionDefinitionAttribtue 将其分配给一个集合然后是所有具有 Collection 的测试类具有相同连接名称的属性将共享灯具的相同实例。

首先用一个简单的例子来证明一个Fixture每个测试类只实例化一次

public class SampleFixture:IDisposable
{
public static int ConstructorCount { get; set; }
public static int DisposeCount { get; set; }

public SampleFixture()
{
ConstructorCount++;
}


public void Dispose()
{
DisposeCount++;
}
}

public class SampleTestClass1 : IClassFixture<SampleFixture>
{
private readonly ITestOutputHelper outputHelper;
private readonly SampleFixture sampleFixture;

public SampleTestClass1(ITestOutputHelper outputHelper, SampleFixture sampleFixture)
{
this.outputHelper = outputHelper;
this.sampleFixture = sampleFixture;
}
[Fact]
public void Test1()
{
this.outputHelper.WriteLine($"Test1 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}");
Assert.Equal(1,SampleFixture.ConstructorCount);
}

[Fact]
public void Test2()
{
this.outputHelper.WriteLine($"Test2 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}");
Assert.Equal(1,SampleFixture.ConstructorCount);

}
}

运行测试,它们都通过了。 1 通过构造函数意味着测试类中的两个测试都使用我的夹具的相同实例。酷 bean 。

现在让我们创建一个 CollectionFixture , 添加另一个测试类并用相同的 Collection 装饰它们属性。

[CollectionDefinition("SampleFixture")]
public class SampleFixtureCollection : ICollectionFixture<SampleFixture>
{

}
public class SampleFixture:IDisposable
{
public static int ConstructorCount { get; set; }
public static int DisposeCount { get; set; }

public SampleFixture()
{
ConstructorCount++;
}


public void Dispose()
{
DisposeCount++;
}
}

[Collection("SampleFixture")]
public class SampleTestClass1 : IClassFixture<SampleFixture>
{
private readonly ITestOutputHelper outputHelper;
private readonly SampleFixture sampleFixture;

public SampleTestClass1(ITestOutputHelper outputHelper, SampleFixture sampleFixture)
{
this.outputHelper = outputHelper;
this.sampleFixture = sampleFixture;
}
[Fact]
public void Test1()
{
this.outputHelper.WriteLine($"Test1 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}");
Assert.Equal(1,SampleFixture.ConstructorCount);
}

[Fact]
public void Test2()
{
this.outputHelper.WriteLine($"Test2 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}");
Assert.Equal(1,SampleFixture.ConstructorCount);

}
}

[Collection("SampleFixture")]
public class SampleTestClass2 : IClassFixture<SampleFixture>
{
private readonly ITestOutputHelper outputHelper;
private readonly SampleFixture sampleFixture;

public SampleTestClass2(ITestOutputHelper outputHelper, SampleFixture sampleFixture)
{
this.outputHelper = outputHelper;
this.sampleFixture = sampleFixture;
}
[Fact]
public void Test1()
{
this.outputHelper.WriteLine($"Test1 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}");
Assert.Equal(1,SampleFixture.ConstructorCount);
}

[Fact]
public void Test2()
{
this.outputHelper.WriteLine($"Test2 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}");
Assert.Equal(1,SampleFixture.ConstructorCount);

}
}

现在我所有的测试都失败了。从第二个测试类中失败的测试中查看此消息:

Test1 - Constructor Count: 3 DisposeCount: 1

看起来构造函数被调用了 3 次! 一次是在 fixture 集合启动期间,然后是每个测试类一次。

这里有什么?我的两个测试都绑定(bind)到同一个集合,但每个测试类都获得了自己的测试夹具实例。

我是不是哪里做错了或者这是一个错误? (我在 GitHub 中没有发现它)。

将 xUnit v2.4.1 与 .net core 2.1 结合使用

最佳答案

我想通了。

使用 Fixture 集合时,测试类不应实现 IClassFixture 接口(interface)。

改变

[Collection("SampleFixture")]
public class SampleTestClass1 : IClassFixture<SampleFixture>
{...}

[Collection("SampleFixture")]
public class SampleTestClass1 // <-- Removed IClassFixture<SampleFixture>
{...}

解决了问题。

关于c# - XUnit CollectionFixture 实例化多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58143467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com