Home

Awesome

<p align="center"> <a href="https://github.com/Issafalcon/neotest-dotnet/actions/workflows/main.yml"> <img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/Issafalcon/neotest-dotnet/main.yml?label=main&style=for-the-badge"> </a> <a href="https://github.com/Issafalcon/neotest-dotnet/releases"> <img alt="GitHub release (latest SemVer)" src="https://img.shields.io/github/v/release/Issafalcon/neotest-dotnet?style=for-the-badge"> </a> </p>

Neotest .NET

Neotest adapter for dotnet tests

Pre-requisites

neotest-dotnet requires makes a number of assumptions about your environment:

  1. The dotnet sdk that is compatible with the current project is installed and the dotnet executable is on the users runtime path (future updates may allow customisation of the dotnet exe location)
  2. The user is running tests using one of the supported test runners / frameworks (see support grid)
  3. (For Debugging) netcoredbg is installed and nvim-dap plugin has been configured for netcoredbg (see debug config for more details)
  4. Requires nvim-treesitter and the parser for C#.

Installation

Packer

  use({
    "nvim-neotest/neotest",
    requires = {
      {
        "Issafalcon/neotest-dotnet",
      },
    }
  })

vim-plug

    Plug 'https://github.com/nvim-neotest/neotest'
    Plug 'https://github.com/Issafalcon/neotest-dotnet'

Usage

require("neotest").setup({
  adapters = {
    require("neotest-dotnet")
  }
})

Additional configuration settings can be provided:

require("neotest").setup({
  adapters = {
    require("neotest-dotnet")({
      dap = {
      -- Extra arguments for nvim-dap configuration
      -- See https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for values
        args = {justMyCode = false },
      -- Enter the name of your dap adapter, the default value is netcoredbg
        adapter_name = "netcoredbg"
      },
      -- Let the test-discovery know about your custom attributes (otherwise tests will not be picked up)
      -- Note: Only custom attributes for non-parameterized tests should be added here. See the support note about parameterized tests
      custom_attributes = {
        xunit = { "MyCustomFactAttribute" },
        nunit = { "MyCustomTestAttribute" },
        mstest = { "MyCustomTestMethodAttribute" }
      },
      -- Provide any additional "dotnet test" CLI commands here. These will be applied to ALL test runs performed via neotest. These need to be a table of strings, ideally with one key-value pair per item.
      dotnet_additional_args = {
        "--verbosity detailed"
      },
      -- Tell neotest-dotnet to use either solution (requires .sln file) or project (requires .csproj or .fsproj file) as project root
      -- Note: If neovim is opened from the solution root, using the 'project' setting may sometimes find all nested projects, however,
      --       to locate all test projects in the solution more reliably (if a .sln file is present) then 'solution' is better.
      discovery_root = "project" -- Default
    })
  }
})

Additional dotnet test arguments

As well as the dotnet_additional_args option in the adapter setup above, you may also provide additional CLI arguments as a table to each neotest command. By doing this, the additional args provided in the setup function will be replaced in their entirety by the ones provided at the command level.

For example, to provide a runtime argument to the dotnet test command, for all the tests in the file, you can run:

require("neotest").run.run({ vim.fn.expand("%"), dotnet_additional_args = { "--runtime win-x64" } })

NOTE:

Debugging

Debugging Using neotest dap strategy

local install_dir = path.concat{ vim.fn.stdpath "data", "mason" }

dap.adapters.netcoredbg = {
  type = 'executable',
  command = install_dir .. '/packages/netcoredbg/netcoredbg',
  args = {'--interpreter=vscode'}
}

Neotest-Dotnet uses a custom strategy for debugging, as netcoredbg needs to attach to the running test. The test command is modified by setting the VSTEST_HOST_DEBUG env variable, which then waits for the debugger to attach.

To use the custom strategy, you no longer need to provide a custom command other than the standard neotest recommended one for debugging:

The adapter will replace the standard dap strategy with the custom one automatically.

Framework Support

The adapter supports NUnit, xUnit and MSTest frameworks, to varying degrees. Given each framework has their own test runner, and specific features and attributes, it is a difficult task to support all the possible use cases for each one.

To see if your use case is supported, check the grids below. If it isn't there, feel free to raise a ticket, or better yet, take a look at how to contribute and raise a PR to support your use case!

Key

:heavy_check_mark: = Fully supported

:part_alternation_mark: = Partially Supported (functionality might behave unusually)

:interrobang: = As yet untested

:x: = Unsupported (tested)

NUnit

Framework FeatureScope LevelDocsStatusNotes
Test (Attribute)MethodTest - Nunit:heavy_check_mark:Supported when used inside a class with or without the TestFixture attribute decoration
TestFixture (Attribute)ClassTestFixture - Nunit:heavy_check_mark:
TestCase() (Attribute)MethodTestCase - Nunit:heavy_check_mark:Support for parameterized tests with inline parameters. Supports neotest 'run nearest' and 'run file' functionality
Nested ClassesClass:heavy_check_mark:Fully qualified name is corrected to include + when class is nested
Theory (Attribute)MethodTheory - Nunit:x:Currently has conflicts with XUnits Theory which is more commonly used
TestCaseSource (Attribute)MethodTestCaseSource - NUnit:heavy_check_mark:Bundles all dynamically parameterized tests under one neotest listing (short output contains errors for all tests. One test failure displays failure indicator for entire test "grouping"). Supports neotest 'run nearest' and 'run file' functionality

xUnit

Framework FeatureScope LevelDocsStatusNotes
Fact (Attribute)MethodFact - xUnit:heavy_check_mark:
Theory (Attribute)MethodTheory - xUnit:heavy_check_mark:Used in conjunction with the InlineData() attribute
InlineData() (Attribute)MethodTheory - xUnit:heavy_check_mark:Support for parameterized tests with inline parameters. Supports neotest 'run nearest' and 'run file' functionality
ClassData() (Attribute)MethodClassData - xUnit:heavy_check_mark:Bundles all dynamically parameterized tests under one neotest listing (short output contains errors for all tests. One test failure displays failure indicator for entire test "grouping"). Supports neotest 'run nearest' and 'run file' functionality
Nested ClassesClass:heavy_check_mark:Fully qualified name is corrected to include + when class is nested

MSTest

Framework FeatureScope LevelDocsStatusNotes
TestMethod (Attribute)MethodTestMethod - MSTest:heavy_check_mark:
TestClass (Attribute)ClassTestClass - MSTest:heavy_check_mark:
Nested ClassesClass:heavy_check_mark:Fully qualified name is corrected to include + when class is nested
DataTestMethod (Attribute)MethodDataTestMethod - MSTest:heavy_check_mark:
DataRow (Attribute)MethodDataRow - MSTest:heavy_check_mark:Support for parameterized tests with inline parameters. Supports neotest 'run nearest' and 'run file' functionality

Limitations

  1. A tradeoff was made between being able to run parameterized tests and the specificity of the dotnet --filter command options. A more lenient 'contains' type filter is used in order for the adapter to be able to work with parameterized tests. Unfortunately, no amount of formatting would support specific FullyQualifiedName filters for the dotnet test command for parameterized tests.
  2. Dynamically parameterized tests need to be grouped together as neotest-dotnet is unable to robustly match the full test names that the .NET test runner attaches to the tests at runtime.
    • An attempt was made to use dotnet test -t to extract the dynamic test names, but this was too unreliable (duplicate test names were indistinguishable, and xUnit was the only runner that provided fully qualified test names)
  3. See the support guidance for feature and language support
  1. As mentioned in the Debugging section, there are some discrepancies in test output at the moment.

NUnit Limitations

  1. Using the [Test] attribute alongside [TestCase] attributes on the same method will cause neotest-dotnet to duplicate the item with erroneous nesting in the test structure. This will also break the ability of neotest to run the test cases e.g:
    [Test]
    [TestCase(1)]
    [TestCase(2)]
    public void Test_With_Parameters(int a)
    {
        Assert.AreEqual(2, a);
    }

Contributing

Any help on this plugin would be very much appreciated. It has turned out to be a more significant effort to account for all the Microsoft dotnet test quirks and various differences between each test runner, than I had initially imagined.

First Steps

If you have a use case that the adapter isn't quite able to cover, a more detailed understanding of why can be achieved by following these steps:

  1. Setting the loglevel property in your neotest setup config to 1 to reveal all the debug logs from neotest-dotnet
  2. Open up your tests file and do what your normally do to run the tests
  3. Look through the neotest log files for logs prefixed with neotest-dotnet (can be found by running the command echo stdpath("log"))
  4. You should be able to piece together how the nodes in the neotest summary window are created (Using logs from tests that are "Found")

Usually, if tests are not appearing in the neotest summary window, or are failing to be discovered by individual or grouped test runs, there will usually be an issue with one of the above steps. Carefully examining the names in the original node list and the names of the tests in each of the result lists, usually highighlights a mismatch.

  1. Narrow down the function where you think the issue is.
  2. Look through the unit tests (named by convention using <filename_spec.lua>) and check if there is a test case covering the use case for your situation
  3. Write a test case that would enable your use case to be satisfied
  4. See that the test fails
  5. Try to fix the issue until the test passes

Running Tests

To run the plenary tests from CLI, in the root folder, run

make test