unit-testingvisual-studio-codetest-explorer

VS Code - Test Explorer no longer showing any tests. Error: "Cannot read properties of null"


What's Not working

At some point, the lists of tests that used to show up in Test Explorer are now no longer appearing. When I try to refresh the list, I get an error that the system is encountering a null value somewhere.

I have the following setup:

VSCODE

Version: 1.70.2 (user setup)
Commit: e4503b30fc78200f846c62cf8091b76ff5547662
Date: 2022-08-16T05:35:13.448Z
Electron: 18.3.5
Chromium: 100.0.4896.160
Node.js: 16.13.2
V8: 10.0.139.17-electron.0
OS: Windows_NT x64 10.0.19044

Widget Solution Setup:

I have a main project, and a test project organized like this:

enter image description here

Widgets.Server Project file:

In part, this is what the project file contains - NB: the platform

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <IsPackable>true</IsPackable>

This is what the Test project file contains - matching platform:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
    <PackageReference Include="TagLibSharp" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.1.2">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Moq" Version="4.17.2" />
    <PackageReference Include="Shouldly" Version="4.0.3" />
    <PackageReference Include="xunit" Version="2.4.1" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Widgets.Server\Widgets.Server.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

settings.json

In my .vscode folder which lives at the same level as the server folder, this is what the settings file contains: (which is correct as far as path to the test project)

{
  "dotnet-test-explorer.testProjectPath": "Widgets.Server.Tests/Widgets.Server.Tests.csproj"
}

Cannot Read Properties Error enter image description here

Manually Running Tests

When I open up a specific test class, and run it, it seems to work. Please see the picture below: enter image description here

EDIT 1

The other behavior I'm noticing is that when I first open VSCOde, it finds all the tests and lists the total at the bottom status bar. But sometimes, when I try to debug just one test, I get this error:

Failed to start debugger: "System.InvalidOperationException: The debugger could not be started\r\n   at OmniSharp.DotNetTest.Services.DebugTestService.Handle(DebugTestGetStartInfoRequest request) in D:\\a\\1\\s\\src\\OmniSharp.DotNetTest\\Services\\DebugTestService.cs:line 42\r\n   at OmniSharp.Endpoint.EndpointHandler`2.GetFirstNotEmptyResponseFromHandlers(ExportHandler`2[] handlers, TRequest request) in D:\\a\\1\\s\\src\\OmniSharp.Host\\Endpoint\\EndpointHandler.cs:line 198\r\n   at OmniSharp.Endpoint.EndpointHandler`2.HandleRequestForLanguage(String language, TRequest request, RequestPacket packet) in D:\\a\\1\\s\\src\\OmniSharp.Host\\Endpoint\\EndpointHandler.cs:line 234\r\n   at OmniSharp.Endpoint.EndpointHandler`2.Process(RequestPacket packet, LanguageModel model, JToken requestObject) in D:\\a\\1\\s\\src\\OmniSharp.Host\\Endpoint\\EndpointHandler.cs:line 143\r\n   at OmniSharp.Stdio.Host.HandleRequest(String json, ILogger logger) in D:\\a\\1\\s\\src\\OmniSharp.Stdio\\Host.cs:line 258"

And in the output window i see this:

----- Debugging test method Widgets.Server.Tests.FeatureToggleSampleControllerShould.Return_String_If_Toggle_Enabled -----

MSBuild version 17.3.0+92e077650 for .NET

C:\Program Files\dotnet\sdk\6.0.400\Microsoft.Common.CurrentVersion.targets(4809,5): error MSB3021: Unable to copy file "C:\Users\me\.nuget\packages\microsoft.testplatform.objectmodel\17.1.0\lib\netcoreapp2.1\Microsoft.TestPlatform.PlatformAbstractions.dll" to "bin\Debug\net5.0-windows\Microsoft.TestPlatform.PlatformAbstractions.dll". The process cannot access the file 'c:\Users\me\Documents\src\codeReview\widgets\server\Widgets.Server.Tests\bin\Debug\net5.0-windows\Microsoft.TestPlatform.PlatformAbstractions.dll' because it is being used by another process. [c:\Users\me\Documents\src\codeReview\widgets\server\Widgets.Server.Tests\Jw.O3M.Server.Tests.csproj]
    10 Warning(s)
    2 Error(s)

When this happens, I have to delete the bin folder in the Test project and also find and kill the testhost process. And then I can run a single test by clicking on the "Run Test | Debug Test" options under the [Fact] decorator

But the test explorer remains empty. No matter what I do i cannot get the list of tests to appear.


Solution

  • This is a know issue in the Test Explorer extension, see https://github.com/formulahendry/vscode-dotnet-test-explorer/issues/370

    A workaround is to show the tests in a 'flat' Tree Mode view: Screenshot of settings in VSCode