Visual Studio Online中的数据库集成测试

tra*_*max 15 database sql-server integration-testing azure-devops

我在Visual Studio Online中享受新的Build工具.允许我做几乎我做本地构建服务器的所有事情.但是我缺少的一件事是集成数据库测试:对于每次构建运行,我都会从脚本重新创建测试数据库并对其进行数据库测试.

在Visual Studio Online中,我似乎无法找到满足我需求的任何数据库实例.

我尝试为每次构建运行创建Azure SQL数据库(通过PowerShell),然后在构建完成后删除它.但是创建数据库需要永远(与构建过程的其余部分相比).即使完成PowerShell脚本,数据库还没有准备好接受请求 - 我需要不断检查它是否真的准备好了.因此,这种情况变得过于复杂且不可靠.

是否有其他选项可以在Visual Studio Online中进行数据库(SQL Server)集成测试?

更新:我想我不太清楚我需要什么 - 我需要一个免费的(非常便宜的)SQL Server实例来连接到在VSO中的构建代理上运行的实例.像SQL Express或SQL CE或LocalDB之类的东西,我可以连接到它并重新创建数据库来运行C#测试.重新创建数据库或运行测试不是问题,拥有有效的连接字符串是一个问题.

2016年10月更新: 我发表了关于如何在VSTS中进行集成测试的博文

Rom*_*nev 20

TFS构建服务器预装了MSSQL Server 2012和MSSQL Server 2014 LocalDB.

来源: TFS服务 - 托管构建服务器上的软件

因此,只需将以下单行内容放入解决方案的构建后事件中,即可根据需要创建MYTESTDB LocalDB实例.这将允许您连接到(LocalDB)\MYTESTDB运行数据库集成测试就好了.

"C:\Program Files\Microsoft SQL Server\120\Tools\Binn\SqlLocalDB.exe" create "MYTESTDB" 12.0 -s
Run Code Online (Sandbox Code Playgroud)

来源: SqlLocalDB实用程序

  • @JoseAlonsoMonge喜欢这样:`Server =(localdb)\ v12.0; Database = MyTestingDbName` (2认同)

Nor*_*ino 5

Azure DevOps 中,通过.net Core 和 EF Core,我使用了不同的技术。我在内存数据库中使用 SQLite 来执行集成和端到端测试。目前在 .net Core 中,您可以使用 InMemory 数据库和 SQLite 与内存选项,在默认的 Azure DevOps CI 代理中运行任何集成测试。

InMemory : https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory 请注意,InMemory 数据库不是关系数据库,它是一个多用途的数据库,只是提一下一个限制:

InMemory 将允许您在关系数据库中保存违反参照完整性约束的数据

内存模式下的 SQLite https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite 这种方法提供了一个更现实的测试平台。

现在,我更进一步,我不希望只能够在 Azure DevOps 中运行具有数据库依赖项的集成测试,我还希望能够在 CI 代理中托管我的 WebAPI,并在API DBcontext 和我的 Persister 对象(Persister 对象是一个帮助类,它允许我自动生成任何类型的实体并将它们保存到数据库中)。


关于集成测试和 Ent 到 End 测试的快速说明:

集成测试

涉及数据库的集成测试的示例可以是数据访问层的测试。在这种情况下,通常情况下,您会在开始测试时创建一个 DBContext,用一些数据填充目标数据库,使用被测组件来操作数据,然后再次使用 DBContext 来确保满足断言。这个场景非常简单,在相同的代码中,您可以共享相同的 DBContext 来生成数据并将其注入到组件中。

端到端测试

想象一下,在我的例子中,您想要测试一个 RESTful .net Core WebAPI,确保所有 CRUD 操作都按预期工作,并且您想要测试过滤、分页等是否正确。在这种情况下,在测试(数据设置和/或验证)和 WebAPI 堆栈之间共享相同的 DBContext 会更加复杂。


在 .net EF Core 和 WebHostBuilder 之前

到目前为止,我所知道的唯一可能的方法是拥有一个专用服务器、VM 或 Docker 映像,负责为 API 提供服务,而且还必须可以从 Web 或 Azure DevOps 访问这些 API。设置我的集成测试以重新创建数据库,或者足够聪明/有限以完全忽略现有数据,并确保每个测试对数据损坏具有弹性并且完全可靠(没有假阴性或假阳性结果)。然后我必须配置我的构建定义来运行测试。

使用 cache=shared 和 WebHostBuilder 在内存中利用 SQLite

下面我首先描述我使用的两个主要技术,然后我添加一些代码来展示如何去做。

SQLite 文件::内存:?缓存=共享

SQLite 允许您在内存中工作,而不是使用传统的文件,这已经给我们带来了巨大的性能提升,消除了 I/O 瓶颈,但除此之外,使用选项 cache=shared,我们可以在其中使用多个连接相同的进程访问相同的数据。如果您需要多个数据库,您可以指定一个名称。 更多信息: https : //www.sqlite.org/inmemorydb.html

网络主机生成器

.net Core 提供主机构建器,WebHostBuilder允许我们创建一个服务器来启动和托管我们的 WebAPI,这样就可以像托管在真实服务器上一样访问它。当您在测试类中使用 WebHostBuilder 时,这两个位于同一个进程中。 更多信息: https : //docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilder?view=aspnetcore-2.2

解决方案

在初始化 E2E 测试时,创建一个新客户端来连接 api,创建一个 dbcontext,您将使用它来为数据库设置种子并进行断言。

测试初始化

[TestClass]
public class CategoryControllerTests
{
    private TestServerApiClient _client;
    private Persister<Category> _categoryPersister;
    private Builder<Category> _categoryBuilder;
    private IHouseKeeperContext _context;
    protected IDbContextTransaction Transaction;

    [TestInitialize]
    public void TestInitialize()
    {            
        _context = ContextProvider.GetContext();
        _client = new TestServerApiClient();
        ContextProvider.ResetDatabase();
        _categoryPersister = new Persister<Category>(_context);
        _categoryBuilder = new Builder<Category>();
    }

    [TestCleanup]
    public void Cleanup()
    {
        _client?.Dispose();
        _context?.Dispose();
        _categoryPersister?.Dispose();
        ContextProvider.Dispose();            
    }
    [...]
}
Run Code Online (Sandbox Code Playgroud)

TestServerApiClient类:

public class TestServerApiClient : System.IDisposable
{
    private readonly HttpClient _client;
    private readonly TestServer _server;

    public TestServerApiClient()
    {            
        var webHostBuilder = new WebHostBuilder();
        webHostBuilder.UseEnvironment("Test");
        webHostBuilder.UseStartup<Startup>();

        _server = new TestServer(webHostBuilder);            
        _client = _server.CreateClient();
    }

    public void Dispose()
    {
        _server?.Dispose();
        _client?.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

ContextProvider 类用于生成 DBContext,该 DBContext 可用于为断言提供种子数据或执行数据库查询。

public static class ContextProvider
{
    private static bool _requiresDbDeletion;

    private static IConfiguration _applicationConfiguration;
    public static IConfiguration ApplicationConfiguration
    {
        get
        {
            if (_applicationConfiguration != null) return _applicationConfiguration;

            _applicationConfiguration = new ConfigurationBuilder()
                .AddJsonFile("Config/appsettings.json", optional: false, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            return _applicationConfiguration;
        }
    }
    private static ServiceProvider _serviceProvider;
    public static ServiceProvider ServiceProvider
    {
        get
        {
            if (_serviceProvider != null) return _serviceProvider;

            var serviceCollection = new ServiceCollection();
            serviceCollection.AddSingleton<IConfiguration>(ApplicationConfiguration);
            var databaseType = ApplicationConfiguration?.GetValue<DatabaseType>("DatabaseType") ?? DatabaseType.SQLServer;                
            _requiresDbDeletion = databaseType == DatabaseType.SQLServer;

            IocConfig.RegisterContext(serviceCollection, null);

            _serviceProvider = serviceCollection.BuildServiceProvider();
            return _serviceProvider;
        }
        set
        {
            _serviceProvider = value;
        }
    }

    /// <summary>
    /// Generate the db context
    /// </summary>
    /// <returns>DB Context</returns>
    public static IHouseKeeperContext GetContext()
    {            
        return ServiceProvider.GetService<IHouseKeeperContext>();
    }

    public static void Dispose()
    {
        ServiceProvider?.Dispose();
        ServiceProvider = null;
    }

    public static void ResetDatabase()
    {
        if (_requiresDbDeletion)
        {
            GetContext()?.Database?.EnsureDeleted();
            GetContext()?.Database?.EnsureCreated();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

IocConfig 类是我在框架中用于设置依赖项注入的辅助类。上面使用的方法RegisterContext 负责注册DBContext 并根据需要进行设置,并且由于这是WebAPI 使用的同一类,因此使用配置DatabaseType 来确定要执行的操作。在这个类中,您可能会发现大部分“复杂性”。在内存中使用 SQLite 时,您必须记住:

  1. 连接未打开,自动关闭使用SQL Server时一样(这就是为什么我使用的:context.Database.OpenConnection();
  2. 如果没有连接处于活动状态,数据库将被删除(这就是我使用的原因services.AddSingleton<IHouseKeeperContext>(s ...,重要的是一个连接保持打开状态以便数据库不会被破坏,但另一方面,您必须小心在测试结束时关闭所有连接,以便数据库最终被销毁,下一个测试将正确创建一个新的空数据库。

该类的其余部分处理生产和测试设置的 SQL Server 配置。我可以随时设置测试以使用 SQL Server 的真实实例,所有测试将保持完全独立于其他测试,但它肯定会很慢,并且可能仅适用于每晚构建(如果需要,这取决于系统的大小)。

public class IocConfig
{
    public static void RegisterContext(IServiceCollection services, IHostingEnvironment hostingEnvironment)
    {
        var serviceProvider = services.BuildServiceProvider();
        var configuration = serviceProvider.GetService<IConfiguration>();            
        var connectionString = configuration.GetConnectionString(Constants.ConfigConnectionStringName);
        var databaseType = DatabaseType.SQLServer;

        try
        {
            databaseType = configuration?.GetValue<DatabaseType>("DatabaseType") ?? DatabaseType.SQLServer;
        }catch
        {
            MyLoggerFactory.CreateLogger<IocConfig>()?.LogWarning("Missing or invalid configuration: DatabaseType");
            databaseType = DatabaseType.SQLServer;
        }

        if(hostingEnvironment != null && hostingEnvironment.IsProduction())
        {
            if(databaseType == DatabaseType.SQLiteInMemory)
            {
                throw new ConfigurationErrorsException($"Cannot use database type {databaseType} for production environment");
            }
        }

        switch (databaseType)
        {
            case DatabaseType.SQLiteInMemory:
                // Use SQLite in memory database for testing
                services.AddDbContext<HouseKeeperContext>(options =>
                {
                    options.UseSqlite($"DataSource='file::memory:?cache=shared'");
                });

                // Use singleton context when using SQLite in memory if the connection is closed the database is going to be destroyed
                // so must use a singleton context, open the connection and manually close it when disposing the context
                services.AddSingleton<IHouseKeeperContext>(s => {
                    var context = s.GetService<HouseKeeperContext>();
                    context.Database.OpenConnection();
                    context.Database.EnsureCreated();
                    return context;
                });
                break;
            case DatabaseType.SQLServer:
            default:
                // Use SQL Server testing configuration
                if (hostingEnvironment == null || hostingEnvironment.IsTesting())
                {
                    services.AddDbContext<HouseKeeperContext>(options =>
                    {
                        options.UseSqlServer(connectionString);
                    });

                    services.AddSingleton<IHouseKeeperContext>(s => {
                        var context = s.GetService<HouseKeeperContext>();
                        context.Database.EnsureCreated();
                        return context;
                    });

                    break;
                }

                // Use SQL Server production configuration
                services.AddDbContextPool<HouseKeeperContext>(options =>
                {
                    // Production setup using SQL Server
                    options.UseSqlServer(connectionString);
                    options.UseLoggerFactory(MyLoggerFactory);
                }, poolSize: 5);

                services.AddTransient<IHouseKeeperContext>(service =>
                    services.BuildServiceProvider()
                    .GetService<HouseKeeperContext>());
                break;            
        }
    }
    [...]
}
Run Code Online (Sandbox Code Playgroud)

示例测试,首先我使用持久器生成数据,然后使用 API 获取数据,测试也可以反向,使用 POST 请求设置数据,然后使用 DBContext 读取数据db 并确保创建成功。

[TestMethod]
public async Task GET_support_orderBy_Id()
{
    _categoryPersister.Persist(3, (c, i) =>
    {
        c.Active = 1 % 2 == 0;
        c.Name = $"Name_{i}";
        c.Description = $"Desc_i";
    });

    var response = await _client.GetAsync("/api/category?&orderby=Id");
    var categories = response.To<List<Category>>();

    Assert.That.All(categories).HaveCount(3);
    Assert.IsTrue(categories[0].Id < categories[1].Id &&
                  categories[1].Id < categories[2].Id);

    response = await _client.GetAsync("/api/category?$orderby=Id desc");
    categories = response.To<List<Category>>();

    Assert.That.All(categories).HaveCount(3);
    Assert.IsTrue(categories[0].Id > categories[1].Id &&
                  categories[1].Id > categories[2].Id);
}
Run Code Online (Sandbox Code Playgroud)

结论

我喜欢我可以在 Azure DevOps 中免费运行 E2E 测试的事实,性能非常好,这给了我很大的信心,非常适合想要设置持续交付环境。这是在 Azure DevOps(免费版)中生成执行此代码的部分屏幕截图。 在此处输入图片说明

抱歉,这最终比预期的要长。