Debug nunit tests in a Linux docker container from Visual Studio

北战南征 提交于 2021-01-28 16:31:15

问题


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.

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.

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.

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


回答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

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

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

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

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.

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>

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



来源:https://stackoverflow.com/questions/58347293/debug-nunit-tests-in-a-linux-docker-container-from-visual-studio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!