18

I am trying to execute dotnet test from the command line on our Jenkins build server, but it just hangs on:

Starting test execution, please wait...

It works fine when running this command locally

If I switch to using dotnet xunit it fails with the following:

15:42:57 Locating binaries for framework netcoreapp2.1...
15:42:58 Running .NET Core 2.1 tests for framework netcoreapp2.1...
15:42:58 The specified framework version '2.1' could not be parsed
15:42:58 The specified framework 'Microsoft.NETCore.App', version '2.1' was not found.
15:42:58   - Check application dependencies and target a framework version installed at:
15:42:58       C:\Program Files\dotnet\
15:42:58   - Installing .NET Core prerequisites might help resolve this problem:
15:42:58       http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
15:42:58   - The .NET Core framework and SDK can be installed from:
15:42:58       https://aka.ms/dotnet-download
15:42:58   - The following versions are installed:
15:42:58       2.0.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
15:42:58       2.0.9 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
15:42:58       2.1.2 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
15:42:58       2.1.3 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
15:42:58       2.1.4 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

As per the error message, we have installed the dotnet core SDK on the server but we seem to be missing something.

My test project looks like:

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>    
    <IsPackable>false</IsPackable>
  </PropertyGroup>    
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
    <PackageReference Include="Moq" Version="4.10.0" />
    <PackageReference Include="RichardSzalay.MockHttp" Version="5.0.0" />
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.console" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
    <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
  </ItemGroup>        
</Project>
David Masters
  • 8,069
  • 2
  • 44
  • 75
  • "please be aware that when you add Version="2.1.1" to your project, you will need to have the 2.1.1 runtime installed on your deployment servers for your app to work." - https://github.com/aspnet/Home/issues/3250#issuecomment-403872208 – MUG4N Sep 18 '18 at 19:49

6 Answers6

21

The versioning was a red herring and it turned out to be much simpler problem. My tests were testing Async controller methods, and my non-async tests were doing:

var result = controller.PostAsync(request).Result;

as soon as I changed the tests themselves to use the async/await pattern they worked fine:

var result = await controller.PostAsync(request);

Something that helped me diagnose the issue was using the dotnet test --verbosity d argument. When using this it was outputting some tests that were passing rather than just "Starting test execution, please wait". Interestingly every time I run the command it would execute a different number of tests before appearing to get stuck. This suggested there was perhaps some sort of thread deadlock issue which led me to the solution. I'm still unsure why the command ran fine on my local machine but not our Jenkins agent.

David Masters
  • 8,069
  • 2
  • 44
  • 75
  • 2
    In my case it was the `.Wait()` . Thank you for the tip. – Romesh D. Niriella Aug 22 '19 at 00:27
  • Using .Result was working on my Windows machine but failed on my Linux Jenkins build server, it was within some IAsyncLifetime stuff managed by the XUnit framework which is what I guess it didn't like, thanks a bunch for the lede! – Issung Jul 10 '23 at 04:09
18

Sometimes xUnit tests hang forever because the test runner pipeline can't handle parallel threads.

To check if that's the issue try adding xunit.runner.json file to the tests project root

  <ItemGroup>
    <None Update="xunit.runner.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

And put inside 1 thread config.

{
  "parallelizeTestCollections": true,
  "maxParallelThreads": -1
}
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
  • 2
    I used ` "parallelizeTestCollections": false` to not run the tests in parallel which was my issue. – worldwildwebdev Jul 08 '20 at 11:49
  • How would one approach solving this in order to run tests in parallel? That's exactly what I observed both locally when executing xUnit tests in Rider and also on an Azure release pipeline. – Vladyslav Babych Jun 11 '21 at 08:34
8

The solution that worked for me was to add an AssemblyInfo.cs file with the following contents:

using Xunit;

[assembly: CollectionBehavior(DisableTestParallelization = true)]

Once I did this, the tests started running in my CI builds again (in my case, Azure Pipelines).

Andrew Backes
  • 1,884
  • 4
  • 21
  • 37
4

My problem was an async method inside a constructor:

MyAsyncMethod().GetAwaiter().GetResult();

I fixed the problem by moving the async method out of the constructor and implementing xunits IAsyncLifetime interface (Await Tasks in Test Setup Code in xUnit.net?)

public async Task InitializeAsync()
{
    await MyAsyncMethod();
}
Dorothy Hawley
  • 183
  • 3
  • 6
0

Updating Microsoft.NET.Test.Sdk from 15.9 to 16.3 helped in csproj file.

0

just in case this helps someone, at a random time this line started randomly hanging our tests

_task = Task.Factory.StartNew(async () =>
  {
    await Task.Delay((int)cleanInterval.TotalMilliseconds, _cancellationTokenSource.Token);
-->     while (!_cancellationTokenSource.Token.IsCancellationRequested)
JBoothUA
  • 3,040
  • 3
  • 17
  • 46
  • What did you do to fix it? – Eric Tijerina Oct 19 '22 at 19:46
  • We ended up just removing that code. Lucky for us it wasn't needed anymore – JBoothUA Oct 20 '22 at 20:10
  • 1
    Interesting. I was having an issue with this hanging up during my Azure Pipelines "test" task on a Hosted Service. I ended up having to write a custom service that has a "CancellationTokenSource" property and inject it into the hosted service. Then modify logic in the Hosted service class to "tokenSource.Cancel()" when its detected that this service is running in the pipeline via Environment variables. We changed the while loop to: while (!stoppingToken.IsCancellationRequested && !this.tokenSourceSvc.TokenSource.IsCancellationRequested) – Eric Tijerina Oct 21 '22 at 21:11