0

Given:

I have a project/solution containing tests. It is written in C# and is using nUnit.

What I want:

Somehow to retrieve all the tests in the Project/Solution without executing them and also giving me the CodeFilePath and LineNumber for each test.

What I tried / investigated:

  • dotnet test --list-tests : is giving me nothing (just the displayname)
  • NUnit3TestAdapter : is at somepoint exposing those values (see NUnitTestAdapter). But how can I retrieve them?
Community
  • 1
  • 1
grafbumsdi
  • 405
  • 3
  • 11
  • did you check https://stackoverflow.com/questions/45942674/c-sharp-find-all-tests-in-a-solution – Falco Alexander Feb 25 '20 at 13:19
  • @FalcoAlexander yes, thx. But I also need the exact test name that will be used for the execution then. This is quite hard, because it can be 1. just the function name, 2. a testname property of the attribute OR 3. for combined tests its just the combination of the values :-( In the visual studio test explorer I see that he is smart enough to recognize everything correctly so I thought there must be some way to utilize the NUnit3TestAdapter in a similar way as visual studio does :-/ – grafbumsdi Feb 25 '20 at 13:36
  • 1
    NUnit provides all the info except the file name and line number. The adapter adds those using information from VisualStudio. So, while you might use the adapter as visual studio does, part of what it does is providing that info. You might investigate NavagationDataProvider.cs in the adapter code to see how it uses the DiaSession object from visual studio. – Charlie Feb 25 '20 at 15:55

1 Answers1

0

Thanks @Charlie for pointing out a possible solution. I did basically exactly as you stated.

IMPORTANT NOTICE:

This seems to not work with dot.net CORE tests currently because of this issue: https://github.com/nunit/nunit-console/issues/710

Also DiaSession runs into an exception when used like this in dot.net CORE :-(

using System;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NUnit.Engine;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var testAssemblyPath = @"C:\src\qata\src\tests\external\SomethingProject.Tests\bin\Debug\net461\SomethingProject.Tests.dll";
            var package = new TestPackage(testAssemblyPath);
            var testEngine = new TestEngine();
            var runner = testEngine.GetRunner(package);
            var nUnitXml = runner.Explore(TestFilter.Empty);
            var session = new DiaSession(testAssemblyPath);
            foreach (XmlNode testNode in nUnitXml.SelectNodes("//test-case"))
            {
                var testName = testNode.Attributes["fullname"]?.Value;
                var className = testNode.Attributes["classname"]?.Value;
                var methodName = testNode.Attributes["methodname"]?.Value;
                var navigationData = session.GetNavigationData(className, methodName);
                Console.WriteLine($"{testName} - {navigationData.FileName} - {navigationData.MinLineNumber}.");
            }
        }
    }
}
grafbumsdi
  • 405
  • 3
  • 11