vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

Cannot debug unit tests - Failed to start debugger: "System.NullReferenceException: Object reference not set to an instance of an object

Open mime8 opened this issue 3 years ago • 1 comments

Issue Description

The project I am working on is based on .net 5.0. It uses NUnit (3.12.0) and NUnit3TestAdapter (3.15.1) packages. This issue happens when I try to debug generic NUnit unit tests. It started recently and happens every time I try to click "Debug Test" link above generic unit test. Simple non-generic tests with and without parameters are working fine. I am using portable Visual Studio Code.

When I try to run such tests using "Run test" link, my tests won't run. test-not-running

tests error-when-debugging

Steps to Reproduce

  1. Create simple test class.
  2. Create generic unit test using NUnit (look at example screenshots).
  3. Try to debug the test by clicking "Debug test"

Expected Behavior

I should be able to debug the test

Actual Behavior

I cannot debug the test

Logs

OmniSharp log

[fail]: OmniSharp.Stdio.Host ************ Response (4029.1222ms) ************ { "Request_seq": 284, "Command": "/v2/debugtest/getstartinfo", "Running": true, "Success": false, "Message": "\"System.NullReferenceException: Object reference not set to an instance of an object\\n at OmniSharp.DotNetTest.VSTestManager.DebugGetStartInfoAsync (System.String[] methodNames, System.String runSettings, System.String testFrameworkName, System.String targetFrameworkVersion, System.Threading.CancellationToken cancellationToken) [0x00113] in :0 \\n at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].GetFirstNotEmptyResponseFromHandlers (OmniSharp.Endpoint.Exports.ExportHandler`2[TRequest,TResponse][] handlers, TRequest request) [0x00099] in :0 \\n at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].HandleRequestForLanguage (System.String language, TRequest request, OmniSharp.Protocol.RequestPacket packet) [0x00163] in :0 \\n at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].Process (OmniSharp.Protocol.RequestPacket packet, OmniSharp.Endpoint.LanguageModel model, Newtonsoft.Json.Linq.JToken requestObject) [0x0024b] in :0 \\n at OmniSharp.Stdio.Host.HandleRequest (System.String json, Microsoft.Extensions.Logging.ILogger logger) [0x000f3] in :0 \"", "Body": null, "Seq": 2316, "Type": "response" }

C# log

No logs here.

.NET Test Log

NUnit Adapter 3.15.1.0: Test discovery starting NUnit Adapter 3.15.1.0: Test discovery complete System.ArgumentException: The specified argument cannot be empty. (Parameter 'tests') at Microsoft.VisualStudio.TestPlatform.ObjectModel.ValidateArg.NotNullOrEmpty[T](IEnumerable`1 arg, String parameterName) at Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.TestRunCriteria..ctor(IEnumerable`1 tests, Int64 frequencyOfRunStatsChangeEvent, Boolean keepAlive, String testSettings, TimeSpan runStatsChangeEventTimeout, ITestHostLauncher testHostLauncher, TestSessionInfo testSessionInfo, Boolean debugEnabledForTestSession) at Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers.TestRequestManager.RunTests(TestRunRequestPayload testRunRequestPayload, ITestHostLauncher testHostLauncher, ITestRunEventsRegistrar testRunEventsRegistrar, ProtocolConfig protocolConfig) at Microsoft.VisualStudio.TestPlatform.Client.DesignMode.DesignModeClient.c__DisplayClass23_0.b__0()

Environment information

VSCode version: 1.61.0-insider (1.60.2 too) C# Extension: 1.23.15

Mono Information OmniSharp using built-in mono
Dotnet Information .NET SDK (reflecting any global.json): Version: 5.0.203 Commit: 383637d63f

Runtime Environment: OS Name: pop OS Version: 21.04 OS Platform: Linux RID: linux-x64 Base Path: [omitted]/5.0.203/

Host (useful for support): Version: 5.0.6 Commit: 478b2f8c0e

.NET SDKs installed: 3.1.115 [omitted] 5.0.203 [omitted]

.NET runtimes installed: Microsoft.AspNetCore.App 3.1.15 [omitted] Microsoft.AspNetCore.App 5.0.6 [omitted] Microsoft.NETCore.App 3.1.15 [omitted] Microsoft.NETCore.App 5.0.6 [omitted]

To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download

Visual Studio Code Extensions
Extension Author Version
csharp ms-dotnettools 1.23.15
vsc-community-material-theme Equinusocio 1.4.4

mime8 avatar Sep 29 '21 16:09 mime8

The reason for this is that omnisharp does not consider any Attributtes to run or debug Nunit Tests:

src/OmniSharp.DotNetTest/TestFrameworks/NUnitTestFramework.cs src/OmniSharp.DotNetTest/TestMethodPropertyProvider.cs

The Test Framework is "method-driven". The vscode extension tries to run and debug with the "method name", because that is how the codeLensProvider is implemented, instead, it should use the fully qualified name of the test.

fyi, you can list the tests with fully qualified names with:

dotnet vstest <path/to/test-project.dll> /ListFullyQualifiedTests /ListTestsTargetPath:test.txt

If you want to keep using omnisharp with nunit the way it is, I would recommend you to remove TestCase Attributes.. and create a Test method for each one of them.

rodrigo455 avatar Sep 02 '22 13:09 rodrigo455