Closed jonash871j closed 1 month ago
Hello, which testing framework are you using? This would be ideal case for our new testing.platform. It compiles test projects as executables, so you can very easily put them into docker container. It is supported by MSTest (stable), Xunit (preview), NUnit (preview) and TUnit (stable).
This is how you can dockerize MSTest using the no-docker-file approach:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableMSTestRunner>true</EnableMSTestRunner>
<OutputType>Exe</OutputType>
<!--
docs: https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container
dotnet publish /t:PublishContainer -tl:false
docker run helloworldtests
-->
<IsPublishable>true</IsPublishable>
<EnableSdkContainerSupport>true</EnableSdkContainerSupport>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest" Version="3.6.1" />
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.12.6" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
<ItemGroup>
<!-- Fix for TestingPlatform, before we release 1.5. -->
<ContainerEnvironmentVariable Include="platformOptions__resultDirectory" Value="/tmp/TestResults"></ContainerEnvironmentVariable>
</ItemGroup>
</Project>
The testing platform can still fallback to vstest in dotnet test, and in VS, so you should get great deal of backwards compatibility.
https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-intro
https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-platform-intro?tabs=dotnetcli
Hi, thank you for the quick response, I'm using NUnit. I'll take a look at the new testing platform.
This is the above example changed for NUnit
<!-- file HelloWorldTests.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<!-- Enable Testing.Platform based runner -->
<EnableNUnitRunner>true</EnableNUnitRunner>
<OutputType>exe</OutputType>
<!--
docs: https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container
dotnet publish /t:PublishContainer -tl:false
docker run helloworldtests
-->
<IsPublishable>true</IsPublishable>
<EnableSdkContainerSupport>true</EnableSdkContainerSupport>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0-beta.3" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
<PropertyGroup>
<!-- fix for latest nunit adapter -->
<GenerateSelfRegisteredExtensions>true</GenerateSelfRegisteredExtensions>
</PropertyGroup>
<ItemGroup>
<!-- Customize output dir and hide telemetry notification. -->
<ContainerEnvironmentVariable Include="DOTNET_CLI_TELEMETRY_OPTOUT" Value="1"></ContainerEnvironmentVariable>
<ContainerEnvironmentVariable Include="platformOptions__resultDirectory" Value="/tmp/TestResults"></ContainerEnvironmentVariable>
</ItemGroup>
</Project>
// file UnitTest1.cs
namespace NUnit1
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
Assert.Pass();
}
}
}
I have done a bit of testing now and this seems to do the trick, thank you for the help 😃
I'm trying to run some unit tests in a docker container using dotnet vstest. The catch is that I'm doing it with host networking enabled which gives the container direct access to the host machine.
When I start the tests in the container with
dotnet vstest Settings.Tests/bin/Release/net8.0/Settings.Tests.dll --Diag:log.txt
, it fails and produces this output:By taking a closer look in the log file, vstest seems to be starting a socket server on a randomly selected port and the client afterwards fails to connect to it.
Server logs:
Client logs:
I believe the problem is with network host mode enabled, I'll need to specify which port the server is being hosted on in the docker-compose file, before the client would be able to connect to it. So it's a bit of a problem that the port is randomly selected.
I noticed in the documentation that there is a 'Port' argument that can be parsed to vstest to use a custom port, so something like this
dotnet vstest Settings.Tests/bin/Release/net8.0/Settings.Tests.dll --Port:5126 --Diag:log.txt
. But I haven't been able to get this argument to work both in the docker container, but also locally when I try it out in any random test project.From looking in the logs it seems to have picked the correct specified port, but it's unable to start hosting the vstest server. I have tried to use different ports and tried to disable to firewall, but I still get the same error.