![](/img/trans.png)
[英]Running Unit / Integration tests through an in-memory SQL database using Visual Studio Team Services
[英]Error Running SQL Lite In-Memory Unit Tests in Docker Container
我正在使用 docker 容器在 azure-pipeline 中运行我的 XUnit 测试。 对于每个 .NET 核心单元测试项目,我都有一个 Dockerfile。 我遵循这里详述的模式:
使用 Visual Studio Team Services 和 Docker Compose 运行单元测试
我能够让所有单元测试项目运行,除了我使用以下参考的项目:
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite。
我在 memory 中使用 SQLite。
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var option = new DbContextOptionsBuilder<Context>()
.UseSqlite(connection,
s => {
s.UseNetTopologySuite();
}).Options;
var dbContext = new Context(option, null);
最初,我将 DockerFile 设置如下:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore
但是,在映像中构建和运行时,我收到以下错误:
“无法加载共享库‘libsqlite3-mod-spatialite’或其依赖项之一。”
单元测试在 Visual Studio 测试运行器中运行良好,只是在映像中运行时不行。
经过研究,我将我的 Dockerfile 更改为安装 spatiallite。
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore
RUN apt-get update && apt-get install -y \
libsqlite3-mod-spatialite
我收到以下新错误:
活动测试运行被中止。 原因:测试主机进程崩溃。
在将 SQLite 与空间数据一起使用时,我尝试按照 Microsoft 的建议创建自定义 SQLitePCLRaw 提供程序。
public class NativeLibraryAdapter : IGetFunctionPointer
{
readonly IntPtr _library;
public NativeLibraryAdapter(string name)
=> _library = NativeLibrary.Load(name);
public IntPtr GetFunctionPointer(string name)
=> NativeLibrary.TryGetExport(_library, name, out var address)
? address
: IntPtr.Zero;
}
And in my SQLite configuration:
SQLite3Provider_dynamic_cdecl
.Setup("sqlite3", new NativeLibraryAdapter("sqlite3"));
SQLitePCL.raw.SetProvider(new SQLite3Provider_dynamic_cdecl());
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var option = new DbContextOptionsBuilder<EmployeeContext>()
.UseSqlite(connection,
s => {
s.UseNetTopologySuite();
}).Options;
现在我收到以下错误: “无法加载共享库 'sqlite3' 或其依赖项之一。”
这发生在 Visual Studio 测试运行程序和运行我的 Docker 图像时。
在这一点上,我不确定我是否采取了正确的方法来使其正常工作。 任何指导表示赞赏。
我在使用 mcr.microsoft.com/dotnet/core/sdk:3.1 时遇到了类似的问题。 为了在我的测试用例中运行 SaptiaLite,我必须安装这两个包:
apt-get -y --no-install-recommends install libsqlite3-dev libsqlite3-mod-spatialite
而不是 Microsoft.EntityFrameworkCore.Sqlite,我必须使用
这些包使用系统提供的 SQLite。
Dockerfile 和示例代码可以在这里找到: https://bitbucket.org/assetic/pipelines-dotnet-sonar-spatialite/src/master/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.