List of tests in the project

93 Views Asked by At

I am writing a small C# project that will count the coverage of all tests individually in a project with tests C#. To do this, I need to get a list of all the tests in the project in the FullyQualifiedName format, which can then be passed to the --filter parameter for the dotnet test command. What is the easiest way to do this?

I was hoping that the dotnet test -t(or --list-tests) command would help me, but it only outputs the names of the methods, without specifying which path they are on, and this information is not enough to run each test individually

I've seen a solution for the NUnit framework https://github.com/nunit/nunit3-vs-adapter/discussions/897, but I need it for MSTest and for Xunit

1

There are 1 best solutions below

0
Marat Tim On

I found a solution for Xunit using this https://github.com/xunit/xunit/issues/542

string dllPath = "...";
Assembly.LoadFrom(dllPath);
        
var controller = new XunitFrontController(AppDomainSupport.Denied, dllPath);
using var visitor = new TestDiscoverySink();
controller.Find(false, visitor, TestFrameworkOptions.ForDiscovery());
visitor.Finished.WaitOne();
var tests = visitor.TestCases.Select(testCase => testCase.DisplayName).ToList(); 
visitor.Finished.Dispose();

It doesn't work for MSTest

There is also a problem that due to Assembly.LoadFrom(dllPath), the assembly file is blocked