Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 6, 2026

When running tests in nested classes via VS "Run Tests" command, the filter path was not matching because TUnit used simple class names (NestedClass) instead of the full nested hierarchy with + separator (OuterClass+NestedClass) that VS expects.

Changes

  • TestFilterService.cs: Added GetNestedClassName helper that walks the Parent chain to build full class hierarchy with + separator
  • ReflectionMetadataBuilder.cs: Set Parent property for nested types using DeclaringType
  • MetadataGenerationHelper.cs: Added recursive GenerateParentClassMetadataExpression to correctly populate parent metadata in source-generated mode

Example

Before: Filter path was /Assembly/Namespace/NestedClass/TestMethod
After: Filter path is /Assembly/Namespace/OuterClass+NestedClass/TestMethod

public class OuterClass
{
    public class NestedClass
    {
        [Test]
        public void Inner() { } // Now discoverable via OuterClass+NestedClass filter
    }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: Run Test (by name) in VS fails when test is in a nested class</issue_title>
<issue_description>### Description

When the editor is open and caret positioned within a TUnit test, I can typically right-click within the test and invoke the "Run Tests" command (or press Ctrl+R, T) and the test will build and run, showing results via Test Explorer.

But when that test method is within a nested class, an error occurs before the test is executed.

Expected Behavior

I expect the test to execute like any other test in a non-nested class would.

Actual Behavior

The Output tool window's Tests pane shows this output after a failure:

Building Test Projects
Executing test method: EnumTests+EnumAsStringTests.UnrecognizedName
Not all tests from the test run selection could be discovered.
========== Starting test run ==========
========== Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ==========

Steps to Reproduce

Create a test class with:

public partial class NativeAOTTests
{
	[Test]
	public void Outer()
	{
	}

	public class Nested
	{
		[Test]
		public void Inner()
		{
		}
	}
}

Right-click within the Outer() method and click the Run Tests command from the context menu.
It runs.

Now do the same for the Inner() method. Notice how it fails.

You can also try the command from the class declaration itself (i.e. within the class, but not within a test method). It works for the outer class but not the nested class.

I suspect the problem is that TUnit cannot parse the test or class name when it has a + in it.

TUnit Version

1.6.5

.NET Version

.NET 10

Operating System

Windows

IDE / Test Runner

Visual Studio

Error Output / Stack Trace

error output is under the "Actual results" section.

Additional Context

No response

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE
  • I've confirmed it works when I use xunit in the same IDE.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 6, 2026 20:36
Copilot AI changed the title [WIP] Fix issue with running tests in nested classes Fix nested class test filtering for Visual Studio "Run Tests" command Jan 6, 2026
Copilot AI requested a review from thomhurst January 6, 2026 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Run Test (by name) in VS fails when test is in a nested class

2 participants