99using BenchmarkDotNet . Loggers ;
1010using BenchmarkDotNet . Reports ;
1111using System . Runtime . InteropServices ;
12+ using BenchmarkDotNet . Filters ;
1213
1314namespace BenchmarkDotNet . Tests . Running
1415{
@@ -232,8 +233,8 @@ public void MixedTypes_ThrowsValidationError_WhenNoBenchmarkAttribute(string[]?
232233 #region Assembly Tests
233234 // In this tests there is no config and logger because the logger is initiated at CreateCompositeLogger when the BenchmarkRunInfo[] is empty
234235 // those cannot be inserted using config
235- [ Theory ]
236236
237+ [ Theory ]
237238 [ InlineData ( null ) ]
238239 [ InlineData ( new object [ ] { new string [ ] { " " } } ) ]
239240 public void AssemblyWithoutBenchmarks_ThrowsValidationError_WhenNoBenchmarksFound ( string [ ] ? args )
@@ -308,6 +309,42 @@ public void AssemblyWithBenchmarks_RunsSuccessfully_WhenBenchmarkAttributePresen
308309 Assert . DoesNotContain ( summary . ValidationErrors , validationError => validationError . Message == GetGeneralValidationError ( ) ) ;
309310 }
310311 }
312+
313+ [ Fact ]
314+ public void AssemblyWithBenchmarks_RunsNothing_WhenAllBenchmarksFilteredOutFromOneTypeWithBenchmarkAttributePresent ( )
315+ {
316+ // Create a mock assembly with benchmark types
317+ var assemblyName = new AssemblyName ( "MockAssemblyWithBenchmarks" ) ;
318+ var assemblyBuilder = AssemblyBuilder . DefineDynamicAssembly ( assemblyName , AssemblyBuilderAccess . Run ) ;
319+ var moduleBuilder = assemblyBuilder . DefineDynamicModule ( "MockModule" ) ;
320+
321+ // Create a benchmark type
322+ var benchmarkTypeBuilder = moduleBuilder . DefineType ( "MockBenchmark" , TypeAttributes . Public ) ;
323+ var benchmarkMethod = benchmarkTypeBuilder . DefineMethod ( "Benchmark" , MethodAttributes . Public , typeof ( void ) , Type . EmptyTypes ) ;
324+
325+ // Generate method body
326+ var ilGenerator = benchmarkMethod . GetILGenerator ( ) ;
327+ ilGenerator . Emit ( OpCodes . Ret ) ; // Just return from the method
328+
329+ var benchmarkAttributeCtor = typeof ( BenchmarkAttribute ) . GetConstructor ( new [ ] { typeof ( int ) , typeof ( string ) } ) ;
330+ if ( benchmarkAttributeCtor == null )
331+ throw new InvalidOperationException ( "Could not find BenchmarkAttribute constructor" ) ;
332+ benchmarkMethod . SetCustomAttribute ( new CustomAttributeBuilder (
333+ benchmarkAttributeCtor ,
334+ new object [ ] { 0 , "" } ) ) ;
335+ benchmarkTypeBuilder . CreateType ( ) ;
336+
337+ Summary [ ] summaries = null ;
338+
339+ GetConfigWithLogger ( out var logger , out var config ) ;
340+
341+ config . AddFilter ( new NameFilter ( name => name != "Benchmark" ) ) ; // Filter out only benchmark method on MockBenchmark
342+
343+ summaries = BenchmarkRunner . Run ( assemblyBuilder , config ) ;
344+ Assert . DoesNotContain ( GetValidationErrorForType ( benchmarkTypeBuilder ) , logger . GetLog ( ) ) ;
345+ Assert . Contains ( GetExporterNoBenchmarksError ( ) , logger . GetLog ( ) ) ;
346+ }
347+
311348 #endregion
312349 #region Helper Methods
313350 private string GetValidationErrorForType ( Type type )
@@ -330,6 +367,11 @@ private string GetGeneralValidationError()
330367 return $ "No benchmarks were found.";
331368 }
332369
370+ private string GetExporterNoBenchmarksError ( )
371+ {
372+ return "There are no benchmarks found" ;
373+ }
374+
333375 private void GetConfigWithLogger ( out AccumulationLogger logger , out ManualConfig manualConfig )
334376 {
335377 logger = new AccumulationLogger ( ) ;
0 commit comments