简体   繁体   English

在 Visual Studio 的 Linux docker 容器中调试 nunit 测试

[英]Debug nunit tests in a Linux docker container from Visual Studio

I'm trying to change our .NET Core 3.0 app to run in a Linux container.我正在尝试将我们的 .NET Core 3.0 应用程序更改为在 Linux 容器中运行。 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 机器。

I want to debug the tests within the container from Visual Studio on Windows and haven't been able to do this yet.我想在 Windows 上的 Visual Studio 中调试容器内的测试,但还不能这样做。 From what I've read, there doesn't seem to be a way to debug the test from running dotnet test.根据我的阅读,似乎没有办法通过运行 dotnet 测试来调试测试。 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-console 似乎有可能,但我无法运行,因为它会抛出UnsupportedFrameworkException ,因为我们使用的是 nunit 3.12.0。

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.我也很想听听有关完成相同任务的方法,但使用不同的单元测试框架,我不会因为任何特定原因而与 nunit 绑定。

I'm using mcr.microsoft.com/mssql/server:2019-latest for my container which is ubuntu 16.04.我正在为我的容器使用 mcr.microsoft.com/mssql/server:2019-latest,它是 ubuntu 16.04。

You can use .NET Core SDK Linux container to run your tests (version for .NET Core 3.1)您可以使用 .NET Core SDK Linux 容器运行您的测试(Z303CB0EF9EDB9082AZD61BBBE582 版本)

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

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.我建议使用单元测试源代码作为卷安装驱动器,然后将工作目录设置为单元测试 output 路径,其中测试 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 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

See more about dotnet test command to filter by class name, method name etc.请参阅有关按 class 名称、方法名称等过滤的dotnet 测试命令的更多信息。

To debug you have to define environment variable on Linux VSTEST_HOST_DEBUG=1.要调试,您必须在 Linux VSTEST_HOST_DEBUG=1 上定义环境变量。 Hence, your command line to debug specific unit test could go like因此,调试特定单元测试的命令行可以像 go

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

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.运行后,它将打印可用于从 Visual Studio 附加到 AcmeUnitTest Linux 容器特定进程的进程 ID。 Select "Connection type" as "Docker (Linux Container)" in "Attach to Process" dialog. Select 在“附加到进程”对话框中“连接类型”为“Docker(Linux 容器)”。

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.此外,为了能够在 Linux 上进行调试,您必须确保程序集的 CSPROJ 文件(包括测试程序集)使用新的可移植 PDB 格式。 Otherwise, your DLL debugging information will not be loaded.否则,您的 DLL 调试信息将无法加载。 Note that the default for 'DebugType' in new CSPROJ files is 'portable'.请注意,新 CSPROJ 文件中“DebugType”的默认值为“便携式”。 So, you can either delete the line(s) with 'DebugType' or edit as described below.因此,您可以删除带有“DebugType”的行或如下所述进行编辑。

<DebugType>portable</DebugType>

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 中的集成功能,而不必手动执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio 2010中从Linux进行远程编译和调试 - How to remotely compile and debug in Linux from within Visual Studio 2010 使用 Visual Studio 在 Linux Docker 中调试远程 .NET Core 控制台应用程序失败 - Debug remote .NET Core console app in Linux Docker fails with Visual Studio Visual Studio 的“Docker 调试”无法安装 linux 包,但是发布/发布解决并安装了包 - Visual Studio's "Docker Debug" fails to install linux package, however release/publish resolves and installs the package 使用visual studio 2017调试已运行的Docker Linux .net核心容器 - Debugging already running Docker Linux .net core container with visual studio 2017 将环境变量从Linux传递到Docker容器 - Pass environment variable from linux to docker container 如何在Linux中的Mono下调试Visual Studio .NET应用程序? - How to debug Visual Studio .NET application under Mono in Linux? 如何使用 Visual Studio 为 Raspberry Pi 调试 Linux TUI 应用程序 - How to debug Linux TUI applications for Raspberry Pi using Visual Studio 在Linux中从CLI运行Visual Studio代码 - Running Visual Studio Code from the CLI in Linux 从Linux主机触发Visual Studio构建 - Triggering a Visual Studio build from a Linux Host Windows Docker容器与Linux Docker容器 - Windows docker container vs Linux docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM