Reo.Core.Xunit.IntegrationTesting 6.0.31746

dotnet add package Reo.Core.Xunit.IntegrationTesting --version 6.0.31746
NuGet\Install-Package Reo.Core.Xunit.IntegrationTesting -Version 6.0.31746
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Reo.Core.Xunit.IntegrationTesting" Version="6.0.31746" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Reo.Core.Xunit.IntegrationTesting --version 6.0.31746
#r "nuget: Reo.Core.Xunit.IntegrationTesting, 6.0.31746"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Reo.Core.Xunit.IntegrationTesting as a Cake Addin
#addin nuget:?package=Reo.Core.Xunit.IntegrationTesting&version=6.0.31746

// Install Reo.Core.Xunit.IntegrationTesting as a Cake Tool
#tool nuget:?package=Reo.Core.Xunit.IntegrationTesting&version=6.0.31746

Xunit.IntegrationTesting

Расширение фреймворка xUnit для выполнения интеграционного тестирования

Использование

Первоначальная настройка

В проекте с тестами необходимо определить файл со следующим содержимым:

using Reo.Core.IntegrationTesting.TestFramework.Mongo;
using Reo.Core.IntegrationTesting.TestFramework.Postgres;
using Reo.Core.Xunit.IntegrationTesting.Attributes;

[assembly:EnableIntegrationTestingFramework]
[assembly:RaiseContainer<PostgresTestContainer<TestingContext>>]
[assembly:RaiseContainer<MongoTestContainer>]

Атрибут EnableIntegrationTestingFramework должен быть указан в обязательном порядке. Он указывает xUnit, что необходимо использовать расширенный тестовый фреймворк вместо обычного.

Атрибут RaiseContainer нужен для того, чтобы при запуске тестов запустился контейнер указанного типа. В прошлом контейнеры запускались при старте каждого тестового класса, теперь запускается единственный контейнер для всех тестов примерно сразу после загрузки сборки.

На данный момент реализованы четыре контейнера (их можно найти в пакете Reo.Core.IntegrationTesting):

  • Postgres (PostgresTestContainer{TDbContext} и PostgresFixture{TDbContext})
  • Mongo (MongoTestContainer и MongoFixture)
  • Redis (RedisTestContainer и RedisFixture)
  • Elastic (ElasticTestContainer и ElasticFixture)
Написание тестов

В тестовом классе необходимо указать какую фикстуру вы хотите использовать.

CollectionFixture

Фикстура создается один раз на запускаемую пачку тестов

// CollectionDefinition.cs

[CollectionDefinition(nameof(PostgresDefinition))]
public sealed class PostgresDefinition : ICollectionFixture<PostgresFixture<TestingDbContext>>
{ }
// TestClass.cs

[Collection(nameof(PostgresDefinition))]
public sealed class TestClass
{
    private readonly PostgresFixture<TestingDbContext> _fixture;

    public TestClass(PostgresFixture<TestingDbContext> fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public void Verify()
    {
        // ...
    }
}

К сожалению, CollectionDefinition необходимо описывать в каждой сборке, иначе xUnit их не увидит (см. документацию xUnit)

ClassFixture

Фикстура создается один раз на запускаемый тестовый класс

public sealed class TestClass : IClassFixture<MongoFixture>
{
    private readonly MongoFixture _fixture;

    public TestClass(MongoFixture fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public void Verify()
    {
        // ...
    }
}

И то, и другое

xUnit не запрещает внедрять IClassFixture и ICollectionFixture одновременно:

[Collection(nameof(PostgresDefinition))]
public sealed class TestClass : IClassFixture<MongoFixture>
{
    // ...

    public TestClass(PostgresFixture<TestingDbContext> postgresFixture, MongoFixture mongoFixture)
    {
    	// ...
    }

    // ...
}

Сидирование данных

Чтобы проинициализировать справочники, вы должны реализовать абстрактный класс ContainerSeeder

public sealed class PostgresSeeder : ContainerSeeder<PostgresFixture<TestingContext>>
{
    /// <inheritdoc />
    public override async Task SeedAsync(PostgresFixture<TestingContext> fixture)
    {
        await using var databaseContext =
            await fixture.DatabaseContextFactory.CreateDbContextAsync();

        databaseContext.References.Add(new()
        {
            Id = Guid.NewGuid(),
            Name = "Profile test"
        });

        await databaseContext.SaveChangesAsync();
    }
}

Сид не должен содержать конструкторов, кроме стандартного. Количество сидов для одной фикстуры не ограничено.

Немного про очистку базы данных после исполнения конкретного теста

Если после каждого теста вы хотите откатывать ее в первоначальное состояние - используйте метод CleanupAsync, определенной у каждой фикстуры:

public sealed class Tests : IClassFixture<PostgresFixture<TestingContext>>, IAsyncLifetime
{
    private readonly PostgresFixture<TestingContext> _fixture;

    public ContainerSeederTests(PostgresFixture<TestingContext> fixture)
        => _fixture = fixture;

    public async Task InitializeAsync()
    {
        await using var databaseContext =
            await _fixture.DatabaseContextFactory.CreateDbContextAsync();

        databaseContext.Entities.Add(new()
        {
            Id = Guid.NewGuid()
        });

        await databaseContext.SaveChangesAsync();
    }

    [Theory]
    [InlineData(1)]
    [InlineData(2)]
    [InlineData(3)]
    public async Task Verify(int _)
    {
        // Благодаря _fixture.CleanupAsync() в базе всегда будет 1 запись, добавленная в InitializeAsync()
    }


    public Task DisposeAsync()
        => _fixture.CleanupAsync();
}

Метод CleanupAsync очищает базу данных и повторно выполняет сидирование справочников

Регистрация артефактов из фикстуры в AutoMocker

При внедрении фикстуры используйте готовые методы расширения:

public sealed class TestClass :
    IClassFixture<PostgresFixture<TestingDbContext>>,
    IClassFixture<MongoFixture>,
    IClassFixture<ElasticFixture>,
    IClassFixture<RedisFixture>
{
    private readonly AutoMocker _mocker = new();

    // ...

    public TestClass(
        PostgresFixture<TestingDbContext> postgresFixture,
        MongoFixture mongoFixture,
        ElasticFixture elasticFixture,
        RedisFixture redisFixture)
    {
    	// ...

        _mocker
            .SetupPostgres(postgresFixture)
            .SetupMongo(mongoFixture)
            .SetupElastic(elasticFixture)
            .SetupRedis(redisFixture);
    }

    // ...
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Reo.Core.Xunit.IntegrationTesting:

Package Downloads
Reo.Core.IntegrationTesting

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.31746 7 5/3/2024
6.0.31745 7 5/3/2024
6.0.31744 2 5/3/2024
6.0.31743 0 5/2/2024
6.0.31742 92 4/27/2024
6.0.31741 91 4/27/2024
6.0.31740 93 4/26/2024
6.0.31739 86 4/26/2024
6.0.31738 93 4/26/2024
6.0.31737 95 4/26/2024
6.0.31735 93 4/25/2024
6.0.31734 86 4/25/2024
6.0.31733 85 4/25/2024
6.0.31732 88 4/25/2024
6.0.31731 74 4/25/2024
6.0.31730 94 4/24/2024
6.0.31729 85 4/24/2024
6.0.31728 90 4/24/2024
6.0.31727 86 4/23/2024
6.0.31726 72 4/23/2024
6.0.31725 87 4/23/2024
6.0.31724 87 4/22/2024
6.0.31723 87 4/22/2024
6.0.31722 96 4/22/2024
6.0.31721 92 4/22/2024
6.0.31720 87 4/22/2024
6.0.31719 85 4/22/2024
6.0.31718 87 4/22/2024
6.0.31717 89 4/22/2024
6.0.31716 85 4/22/2024
6.0.31715 100 4/20/2024
6.0.31714 99 4/19/2024
6.0.31713 77 4/19/2024
6.0.31712 75 4/19/2024
6.0.31711 91 4/19/2024
6.0.31710 77 4/19/2024
6.0.31709 85 4/19/2024
6.0.31708 86 4/18/2024
6.0.31707 81 4/18/2024
6.0.31706 79 4/18/2024
6.0.31705 78 4/17/2024
6.0.31704 99 4/17/2024
6.0.31703 87 4/17/2024
6.0.31702 89 4/17/2024
6.0.31701 85 4/16/2024
6.0.31700 83 4/16/2024
6.0.31699 85 4/16/2024
6.0.31698 78 4/16/2024
6.0.31697 78 4/16/2024
6.0.31696 77 4/16/2024
6.0.31695 73 4/16/2024
6.0.31694 79 4/16/2024
6.0.31693 79 4/16/2024
6.0.31692 76 4/15/2024
6.0.31691 77 4/15/2024
6.0.31690 88 4/15/2024
6.0.31688 95 4/12/2024
6.0.31687 71 4/12/2024
6.0.31686 77 4/12/2024
6.0.31685 76 4/12/2024
6.0.31684 76 4/11/2024
6.0.31683 86 4/10/2024
6.0.31682 79 4/10/2024
6.0.31681 78 4/10/2024
6.0.31680 83 4/10/2024
6.0.31679 74 4/10/2024
6.0.31678 75 4/10/2024
6.0.31677 87 4/9/2024
6.0.31676 91 4/9/2024
6.0.31675 88 4/8/2024
6.0.31674 95 4/8/2024
6.0.31673 97 4/8/2024
6.0.31672 75 4/8/2024
6.0.31671 75 4/8/2024
6.0.31670 86 4/8/2024
6.0.31669 82 4/8/2024
6.0.31668 90 4/5/2024
6.0.31667 91 4/5/2024
6.0.31666 97 4/3/2024
6.0.31665 90 4/3/2024
6.0.31663 94 4/3/2024
6.0.31662 86 4/3/2024
6.0.31661 88 4/2/2024
6.0.31660 91 4/1/2024
6.0.31659 82 4/1/2024
6.0.31658 79 4/1/2024
6.0.31657 84 3/29/2024
6.0.31656 79 3/29/2024
6.0.31655 82 3/29/2024
6.0.31654 90 3/29/2024
6.0.31653 75 3/29/2024
6.0.31651 69 3/29/2024
6.0.31650 75 3/29/2024
6.0.31649 76 3/29/2024
6.0.31648 76 3/29/2024
6.0.31647 74 3/29/2024
6.0.31646 96 3/29/2024
6.0.31645 79 3/28/2024
6.0.31644 81 3/28/2024
6.0.31643 87 3/28/2024
6.0.31642 82 3/28/2024
6.0.31639 85 3/28/2024
6.0.31638 72 3/28/2024
6.0.31637 96 3/27/2024
6.0.31636 118 3/27/2024
6.0.31631 86 3/27/2024
6.0.31626 93 3/26/2024
6.0.31625 98 3/25/2024
6.0.31618 98 3/20/2024
6.0.31617 90 3/20/2024
6.0.31616 101 3/20/2024
6.0.31615 89 3/20/2024
6.0.31614 107 3/19/2024
6.0.31613 103 3/18/2024
6.0.31612 113 3/18/2024
6.0.31611 114 3/18/2024
6.0.31610 107 3/18/2024
6.0.31609 100 3/15/2024
6.0.31608 100 3/14/2024
6.0.31607 112 3/13/2024
6.0.31606 101 3/13/2024
6.0.31605 99 3/13/2024
6.0.31604 97 3/12/2024
6.0.31603 96 3/12/2024
6.0.31602 125 3/7/2024
6.0.31601 103 3/7/2024
6.0.31600 112 3/7/2024
6.0.31599 107 3/6/2024
6.0.31598 98 3/6/2024
6.0.31597 103 3/6/2024
6.0.31596 101 3/6/2024
6.0.31595 112 3/6/2024
6.0.31594 101 3/4/2024
6.0.31593 89 3/4/2024
6.0.31590 97 3/1/2024
6.0.31589 92 3/1/2024
6.0.31588 96 3/1/2024
6.0.31587 86 3/1/2024
6.0.31586 94 3/1/2024
6.0.31585 84 3/1/2024
6.0.31584 96 3/1/2024
6.0.31583 87 3/1/2024
6.0.31582 97 2/29/2024
6.0.31581 90 2/29/2024
6.0.31580 93 2/29/2024
6.0.31579 100 2/29/2024
6.0.31578 94 2/29/2024
6.0.31577 89 2/29/2024
6.0.31576 96 2/29/2024
6.0.31575 97 2/28/2024