Skip to content

Allow ArgumentsSource to reference methods in other types #2744

@JimmyCushnie

Description

@JimmyCushnie

According to the docs for ArgumentsSource:

The source must be within benchmarked type!

It would be nice to lift this restriction. The attribute constructor could have an overload that takes a Type parameter.

For example, NUnit's TestCaseSource does this.

A common pattern in my code is that I have one class which defines all the different cases that a method might encounter. I feed the results of this class into both my unit tests and my benchmarks, to test each case for both correctness and performance. Currently, my benchmark code is bloated with methods that just return immediately with the result of a method in the cases class, which I need to do because ArgumentsSource can't directly access the method I want to use for the arguments. I would love to be able to remove all those methods.

Example code as it currently is:

[Benchmark]
[ArgumentsSource(nameof(MethodACases))]
public MethodAResults BenchmarkMethodA(MethodACase @case)
    => CodeBeingBenchmarked.MethodA(@case.Param1, @case.Param2, @case.Param3);

public static IEnumerable<MethodACase> MethodACases() => DiagnosticsCases.MethodACases();

Example code as I propose it ought to be:

[Benchmark]
[ArgumentsSource(typeof(DiagnosticsCases), nameof(DiagnosticsCases.MethodACases))]
public MethodAResults BenchmarkMethodA(MethodACase @case)
    => CodeBeingBenchmarked.MethodA(@case.Param1, @case.Param2, @case.Param3);

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions