diff --git a/GitVersion.yml b/GitVersion.yml index 65e3e79624..6516428a99 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -7,4 +7,5 @@ ignore: - 9b0b1c0d80692dba7584905554cfb43c92e29782 - 7e0f59874705082e81a0d614abea2709a9f6a98e - 0f32f64e21316fc908af0721864ab3dd1fcebb4b - - cc09c9b221e35d842bea687c96a1594a574b1fac \ No newline at end of file + - cc09c9b221e35d842bea687c96a1594a574b1fac + - 02e60f4e1d7edc5e4e4ab51a5ac8732f3c94c8b5 \ No newline at end of file diff --git a/TUnit.Engine/Discovery/ReflectionTestDataCollector.cs b/TUnit.Engine/Discovery/ReflectionTestDataCollector.cs index 2e7bf5f04a..6726dbf336 100644 --- a/TUnit.Engine/Discovery/ReflectionTestDataCollector.cs +++ b/TUnit.Engine/Discovery/ReflectionTestDataCollector.cs @@ -1005,7 +1005,36 @@ private static bool IsAsyncMethod(MethodInfo method) private static bool IsCompilerGenerated(Type type) { - return type.IsDefined(typeof(CompilerGeneratedAttribute), inherit: false); + // If the type is not marked as compiler-generated, it's not compiler-generated + if (!type.IsDefined(typeof(CompilerGeneratedAttribute), inherit: false)) + { + return false; + } + + // If the type is compiler-generated but contains test methods, allow it + // This handles cases like Reqnroll-generated test classes that should be executed + return !HasTestMethods(type); + } + + private static bool HasTestMethods(Type type) + { + try + { + var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly); + foreach (var method in methods) + { + if (method.IsDefined(typeof(TestAttribute), inherit: false)) + { + return true; + } + } + return false; + } + catch + { + // If we can't access the methods, treat it as not having test methods + return false; + } } private static ParameterInfo[] GetParametersWithoutCancellationToken(MethodInfo method) diff --git a/TUnit.TestProject.VB.NET/CompilerGeneratedTest.vb b/TUnit.TestProject.VB.NET/CompilerGeneratedTest.vb new file mode 100644 index 0000000000..70411a97f1 --- /dev/null +++ b/TUnit.TestProject.VB.NET/CompilerGeneratedTest.vb @@ -0,0 +1,24 @@ +Imports System +Imports System.Runtime.CompilerServices +Imports TUnit.Core + +' This simulates a Reqnroll-generated test class with CompilerGeneratedAttribute + +Public Class CompilerGeneratedTest + + Public Sub GeneratedTestMethod() + ' This test should be executed even though the class is marked as CompilerGenerated + Console.WriteLine("Generated test executed successfully") + End Sub + + + + + Public Sub GeneratedTestWithArguments(a As Integer, b As Integer, expected As Integer) + Dim result = a + b + If result <> expected Then + Throw New Exception($"Expected {expected}, but got {result}") + End If + Console.WriteLine($"Generated test with arguments: {a} + {b} = {result}") + End Sub +End Class \ No newline at end of file diff --git a/TUnit.TestProject/CompilerGeneratedTests.cs b/TUnit.TestProject/CompilerGeneratedTests.cs new file mode 100644 index 0000000000..a9b978b88e --- /dev/null +++ b/TUnit.TestProject/CompilerGeneratedTests.cs @@ -0,0 +1,40 @@ +using System; +using System.Runtime.CompilerServices; +using TUnit.Core; + +namespace TUnit.TestProject; + +// This simulates a Reqnroll-generated test class with CompilerGeneratedAttribute +[CompilerGenerated] +public class CompilerGeneratedTests +{ + [Test] + public void GeneratedTestMethod() + { + // This test should be executed even though the class is marked as CompilerGenerated + Console.WriteLine("Generated test executed successfully"); + } + + [Test] + [Arguments(1, 2, 3)] + [Arguments(2, 3, 5)] + public void GeneratedTestWithArguments(int a, int b, int expected) + { + var result = a + b; + if (result != expected) + { + throw new Exception($"Expected {expected}, but got {result}"); + } + Console.WriteLine($"Generated test with arguments: {a} + {b} = {result}"); + } +} + +// Test that types without test methods are still filtered out +[CompilerGenerated] +public class CompilerGeneratedNonTestClass +{ + public void NotATestMethod() + { + // This class should still be filtered out as it has no test methods + } +} \ No newline at end of file