Unit

Building Custom Method Binders

Method binders expand a test method into zero or more parameterized invocations during ZeDiscovery. Built-in binders like [InlineData], [LoadFile], [LoadFiles], and [LoadFileGroups] all build on the same two abstractions: ZeBinderAttribute<TBinder> and IZeMethodBinder.

1. Implement IZeMethodBinder

IZeMethodBinder inherits IDisposable and defines IEnumerable<MethodBinderInfo> Get(MethodInfo method). Each MethodBinderInfo(string Key, object[] Arguments) produces one distinct ZeTest entry whose name includes the binder key and argument index.

using System.Reflection;
using ZeUnit;

public sealed class CsvDataBinder(CsvDataAttribute attribute) : IZeMethodBinder
{
    public IEnumerable<MethodBinderInfo> Get(MethodInfo method)
    {
        var lines = File.ReadAllLines(attribute.Path).Skip(1);
        var rowIndex = 0;

        foreach (var line in lines)
        {
            var parts = line.Split(',');
            var args = new object[] { int.Parse(parts[0]), int.Parse(parts[1]) };
            yield return new MethodBinderInfo($"{Path.GetFileName(attribute.Path)}#{++rowIndex}", args);
        }
    }

    public void Dispose() { }
}

2. Declare the ZeBinderAttribute<TBinder> Attribute

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class CsvDataAttribute(string path)
    : ZeBinderAttribute<CsvDataBinder>
{
    public string Path { get; } = path;
}

3. Use Your Custom Binder on Test Methods

public class DoublerSuite
{
    [CsvData("Data/doubles.csv")]
    public Fact DoublesInput(int input, int expected) =>
        (input * 2).Is(expected);
}

File Loader Reuse

If your custom binder reads files and wants ZeUnit's automatic deserialization (Stream, string, byte[], XDocument, JsonObject, XML/JSON POCOs), resolve IZeFileLoader implementations in ZeUnit.Binders.FileLoaders just as LoadFileMethodComposer and LoadDirectoryMethodComposer do.