在xUnit 2.x中的测试类上使用多个collectionfixture

时间:2017-03-29 00:13:31

标签: xunit xunit.net

我正在为我的DataAccessRepository(使用实体框架)类编写测试用例。这个类在构造函数中有两个参数。 1)连接对象 2)自动对象

现在,我在xunit中使用collectionFixture在我的测试类中传递DatabaseFixture,但我需要将AutoMapper Fixture传递给同一个测试类。我尝试添加两个集合,一个接一个,但它无效。有人可以请教一下如何在xunit的测试类中使用多个FixtureCollection。

我的单元测试类看起来如下,因为我不能在类上使用两个CollectionFixture属性而抛出错误,

`

[Collection(Traits.DatabaseFixtureCollection)]
    [Collection(Traits.AutomapperFixtureCollection)]
    public class MyAssessmentRepositoryTests
    {
        private readonly IMyAssessmentsRepository _Repo;
        public MyAssessmentRepositoryTests(DatabaseFixture dbFixture,AutomapperFixture amFixture)             
        {
            this._Repo = new MyAssessmentRepository(dbFixture.IcmDbContext,amFixture.Mapper);

        }
 }`

1 个答案:

答案 0 :(得分:2)

请参阅https://xunit.github.io/docs/shared-context.html

单个测试类只能在一个测试集合中(这就是为什么对属性有这样的约束)。

解决方案是声明一个“虚拟”测试集合,该集合声明了这样一个集合中的测试应该通过ICollectionFixture<X>控制访问的两个装置。

当它到位时,Test Class ctor会根据需要提供任何Fixture实例。

(您也可以在测试类级别使用IClassFixture来声明Collection之外的东西[但是这样的Fixtures将在该Test Class中执行测试时上下旋转]而不是在测试集合级别将在整个运行过程中向上/向下旋转一次,并在他们轮流访问集合夹具时交给集合中的所有测试类])