首页 > 解决方案 > 使用不同的配置执行相同的测试

问题描述

我有一个支持多个数据库提供程序(SQL、Sqlite、InMemory)的项目。对于API 测试,出于性能原因,我使用 InMemory DB。对于集成测试,我想为所有提供者运行所有测试,以测试迁移、数据库约束等。

有没有办法配置集成测试以使用不同的配置运行?

[编辑] 构建这样的东西?
https://github.com/xunit/xunit/issues/542 https://github.com/xunit/samples.xunit/blob/master/TestRunner/Program.cs

标签: .net-coreintegration-testingxunit

解决方案


一旦我发现[Theory]解决方案是微不足道的。测试执行现在指定要使用的提供程序。我已经为每个提供者预定义了配置文件;这将被复制到将使用的配置文件并执行测试。

    [SkippableTheory]
    [InlineData("MsSql")]
    [InlineData("Sqlite")]
    [InlineData("InMemory")]
    public async Task DoesNotLoadUnspecifiedNavigationPropertiesTest(string provider)
    {
        Skip.If(provider == "MsSql" && !SkipHelper.IsWindowsOS(), "Ignore when not executing on Windows");

        async Task Test()
        {
            // Perform the test with assertions
        }

        await ExecuteTestForProvider(provider, Test);
    }

    private async Task ExecuteTestForProvider(string provider, Func<Task> test)
    {
        try
        {
            SetConfiguration(provider);
            Initialize();

            await test.Invoke();
        }
        finally
        {
            Teardown();
        }
    }

完整的实现可以在这里找到


推荐阅读