Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ ignore:
- 9b0b1c0d80692dba7584905554cfb43c92e29782
- 7e0f59874705082e81a0d614abea2709a9f6a98e
- 0f32f64e21316fc908af0721864ab3dd1fcebb4b
- cc09c9b221e35d842bea687c96a1594a574b1fac
- cc09c9b221e35d842bea687c96a1594a574b1fac
- 02e60f4e1d7edc5e4e4ab51a5ac8732f3c94c8b5
31 changes: 30 additions & 1 deletion TUnit.Engine/Discovery/ReflectionTestDataCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions TUnit.TestProject.VB.NET/CompilerGeneratedTest.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Imports System
Imports System.Runtime.CompilerServices
Imports TUnit.Core

' This simulates a Reqnroll-generated test class with CompilerGeneratedAttribute
<CompilerGenerated>
Public Class CompilerGeneratedTest
<Test>
Public Sub GeneratedTestMethod()
' This test should be executed even though the class is marked as CompilerGenerated
Console.WriteLine("Generated test executed successfully")
End Sub

<Test>
<Arguments(1, 2, 3)>
<Arguments(2, 3, 5)>
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
40 changes: 40 additions & 0 deletions TUnit.TestProject/CompilerGeneratedTests.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading