在 Visual Studio 的 Linux docker 容器中调试 nunit 测试 [英] Debug nunit tests in a Linux docker container from Visual Studio

查看:84
本文介绍了在 Visual Studio 的 Linux docker 容器中调试 nunit 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我们的 .NET Core 3.0 应用程序更改为在 Linux 容器中运行.我已经到了可以在容器内恢复和构建但测试失败的地步,这是意料之中的.该应用中的某些内容仅适用于 Windows 计算机.

I'm trying to change our .NET Core 3.0 app to run in a Linux container. I've gotten to the point where I can restore and build within the container but the tests are failing, which is expected. There are certain things within the app that were only meant for Windows machines.

我想从 Windows 上的 Visual Studio 调试容器内的测试,但目前还无法执行此操作.从我读过的内容来看,似乎没有办法通过运行 dotnet test 来调试测试.运行 nunit-console 似乎有可能,但我无法运行,因为它抛出 UnsupportedFrameworkException,因为我们使用的是 nunit 3.12.0.

I want to debug the tests within the container from Visual Studio on Windows and haven't been able to do this yet. From what I've read, there doesn't seem to be a way to debug the test from running dotnet test. There seems to be possibilities by running nunit-console but I can't get that run because it throws UnsupportedFrameworkException because we are using nunit 3.12.0.

有什么办法可以做我想做的事吗?我也很想听听完成相同任务的方法,但使用不同的单元测试框架,我并没有因为任何特殊原因与 nunit 相关联.

Is there any way to do what I'm trying to do? I'm also up for hearing about ways of accomplishing this same task but with different unit test frameworks, I'm not tied to nunit for any particular reason.

我的容器使用的是 mcr.microsoft.com/mssql/server:2019-latest,它是 ubuntu 16.04.

I'm using mcr.microsoft.com/mssql/server:2019-latest for my container which is ubuntu 16.04.

推荐答案

您可以使用 .NET Core SDK Linux 容器来运行您的测试(.NET Core 3.1 版本)

You can use .NET Core SDK Linux container to run your tests (version for .NET Core 3.1)

   docker pull mcr.microsoft.com/dotnet/core/sdk:3.1

我建议使用单元测试源代码作为卷安装驱动器,然后将工作目录设置为测试 DLL 所在的单元测试输出路径.假设您的源代码在 D 驱动器上并且您构建输出 d:\src\acmeproject\acmetests\bin\Debug\netcoreapp3.1\acmetests.dll,那么在 Linux 上运行测试的命令行可能会像

I would suggest mounting your drive with the unit tests source code as volume and then setting a working directory to your unit test output path where the test DLL. Assuming your source code is on D drive and you build output d:\src\acmeproject\acmetests\bin\Debug\netcoreapp3.1\acmetests.dll the command line to run tests on Linux could go like

docker run --rm -v d:\:/mnt/d/ -w /mnt/d/src/acmeproject/acmetests/bin/Debug/netcoreapp3.1 mcr.microsoft.com/dotnet/core/sdk:3.1 dotnet test acmetests.dll

查看有关 dotnet test 命令的更多信息按类名、方法名等过滤

See more about dotnet test command to filter by class name, method name etc.

要调试,您必须在 Linux VSTEST_HOST_DEBUG=1 上定义环境变量.因此,您调试特定单元测试的命令行可能会像

To debug you have to define environment variable on Linux VSTEST_HOST_DEBUG=1. Hence, your command line to debug specific unit test could go like

docker run --rm --name AcmeUnitTest -e VSTEST_HOST_DEBUG=1 -v d:\:/mnt/d/ -w /mnt/d/src/acmeproject/acmetests/bin/Debug/netcoreapp3.1 mcr.microsoft.com/dotnet/core/sdk:3.1 dotnet test acmetests.dll --filter Name~AcmeTestMethod

运行后,它将打印进程 ID,您可以使用该 ID 从 Visual Studio 附加到 AcmeUnitTest Linux 容器特定进程.选择连接类型"作为Docker(Linux容器)"在附加到进程"中对话框.

Once you run it will print the process ID that you can use to attach to the AcmeUnitTest Linux container specific process from the Visual Studio. Select "Connection type" as "Docker (Linux Container)" in "Attach to Process" dialog.

此外,为了能够在 Linux 上进行调试,您必须确保程序集的 CSPROJ 文件(包括测试程序集)使用新的可移植 PDB 格式.否则,将不会加载您的 DLL 调试信息.请注意,新 CSPROJ 文件中DebugType"的默认值是portable".因此,您可以使用DebugType"删除行或按如下所述进行编辑.

Also, to be able to debug on Linux you have to make sure that the CSPROJ file of the assemblies, including the test assembly, is using the new portable PDB format. Otherwise, your DLL debugging information will not be loaded. Note that the default for 'DebugType' in new CSPROJ files is 'portable'. So, you can either delete the line(s) with 'DebugType' or edit as described below.

<DebugType>portable</DebugType>

我希望我们很快就能在 Visual Studio 中将此作为集成功能,而不必手动完成.

I expect that we will have this as integrated feature in the Visual Studio some time soon, instead of having to do it manually.

这篇关于在 Visual Studio 的 Linux docker 容器中调试 nunit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆