Skip to content

Latest commit

 

History

History
317 lines (258 loc) · 8.52 KB

parameterised.md

File metadata and controls

317 lines (258 loc) · 8.52 KB

Parameterised Tests

Additions to file name

Every parameterised case has a unique file name with the parameters appended to the file name.

The appending format is _ParamName=ParamValue repeated for each parameter.

A test with two parameters param1 + param2, and called twice with the values value1a+ value2a and value1b+ value2b would have the following file names:

  • MyTest.MyMethod_param1=value1a_param2=value2a.verified.txt
  • MyTest.MyMethod_param1=value1b_param2=value2b.verified.txt

Invalid characters

Characters that cannot be used for a file name will be replaced with a dash (-).

xUnit

InlineData

[Theory]
[InlineData("Value1")]
[InlineData("Value2")]
public Task InlineDataUsage(string arg)
{
    var settings = new VerifySettings();
    settings.UseParameters(arg);
    return Verify(arg, settings);
}

[Theory]
[InlineData("Value1")]
[InlineData("Value2")]
public Task InlineDataUsageFluent(string arg)
{
    return Verify(arg)
        .UseParameters(arg);
}

snippet source | anchor

MemberData

[Theory]
[MemberData(nameof(GetData))]
public Task MemberDataUsage(string arg)
{
    var settings = new VerifySettings();
    settings.UseParameters(arg);
    return Verify(arg, settings);
}

[Theory]
[MemberData(nameof(GetData))]
public Task MemberDataUsageFluent(string arg)
{
    return Verify(arg)
        .UseParameters(arg);
}

public static IEnumerable<object[]> GetData()
{
    yield return new object[] {"Value1"};
    yield return new object[] {"Value2"};
}

snippet source | anchor

Complex MemberData

xUnit only exposes parameter information when the types certain types. For unknown types the parameter information cannot be retrieved from the xUnit context, and instead the parameters need to be explicitly passed in. This is done by calling UseParameters() on the base class.

[UsesVerify]
public class ComplexParametersSample
{
    [ModuleInitializer]
    public static void Initialize()
    {
        VerifierSettings.NameForParameter<ComplexData>(_ => _.Value);
        VerifierSettings.NameForParameter<ComplexStructData>(_ => _.Value);
    }

    [Theory]
    [MemberData(nameof(GetComplexMemberData))]
    public Task ComplexMemberData(ComplexData arg)
    {
        var settings = new VerifySettings();
        settings.UseParameters(arg);
        return Verify(arg, settings);
    }

    [Theory]
    [MemberData(nameof(GetComplexMemberData))]
    public Task ComplexMemberDataFluent(ComplexData arg)
    {
        return Verify(arg)
            .UseParameters(arg);
    }

    [Theory]
    [MemberData(nameof(GetComplexMemberData))]
    public Task ComplexMemberNullableData(ComplexData? arg)
    {
        var settings = new VerifySettings();
        settings.UseParameters(arg);
        return Verify(arg, settings);
    }

    [Theory]
    [MemberData(nameof(GetComplexMemberData))]
    public Task ComplexMemberNullableDataFluent(ComplexData? arg)
    {
        return Verify(arg)
            .UseParameters(arg);
    }

    public static IEnumerable<object[]> GetComplexMemberData()
    {
        yield return new object[]
        {
            new ComplexData {Value = "Value1"}
        };
        yield return new object[]
        {
            new ComplexData {Value = "Value2"}
        };
    }

    public class ComplexData
    {
        public string Value { get; set; } = null!;
    }

    [Theory]
    [MemberData(nameof(GetComplexMemberStructData))]
    public Task ComplexMemberStructData(ComplexStructData arg)
    {
        var settings = new VerifySettings();
        settings.UseParameters(arg);
        return Verify(arg, settings);
    }

    [Theory]
    [MemberData(nameof(GetComplexMemberStructData))]
    public Task ComplexMemberStructDataFluent(ComplexStructData arg)
    {
        return Verify(arg)
            .UseParameters(arg);
    }

    [Theory]
    [MemberData(nameof(GetComplexMemberStructData))]
    public Task ComplexMemberNullableStructData(ComplexStructData? arg)
    {
        var settings = new VerifySettings();
        settings.UseParameters(arg);
        return Verify(arg, settings);
    }

    [Theory]
    [MemberData(nameof(GetComplexMemberStructData))]
    public Task ComplexMemberNullableStructDataFluent(ComplexStructData? arg)
    {
        return Verify(arg)
            .UseParameters(arg);
    }

    public static IEnumerable<object[]> GetComplexMemberStructData()
    {
        yield return new object[]
        {
            new ComplexStructData("Value1")
        };
        yield return new object[]
        {
            new ComplexStructData("Value2")
        };
    }

    public struct ComplexStructData
    {
        public ComplexStructData(string value)
        {
            Value = value;
        }

        public string Value { get; set; } = null!;
    }
}

snippet source | anchor

VerifierSettings.NameForParameter is required since the parameter type has no ToString() override that can be used for deriving the name of the .verified. file.

NUnit

TestCase

[TestCase("Value1")]
[TestCase("Value2")]
public Task TestCaseUsage(string arg)
{
    return Verify(arg);
}

snippet source | anchor

MSTest

MSTest does not expose the parameter values via its extensibility context. So parameter values must passed in via settings.

DataRow

[TestClass]
public class ParametersSample :
    VerifyBase
{
    [DataTestMethod]
    [DataRow("Value1")]
    [DataRow("Value2")]
    public Task DataRowUsage(string arg)
    {
        var settings = new VerifySettings();
        settings.UseParameters(arg);
        return Verify(arg, settings);
    }

    [DataTestMethod]
    [DataRow("Value1")]
    [DataRow("Value2")]
    public Task DataRowUsageFluent(string arg)
    {
        return Verify(arg)
            .UseParameters(arg);
    }
}

snippet source | anchor

Overriding text used for parameters

UseTextForParameters() can be used to override the text used for {Parameters}.

{Directory}/{TestClassName}.{TestMethodName}_{Parameters}_{UniqueFor1}_{UniqueFor2}_{UniqueForX}.verified.{extension}

[Theory]
[InlineData("Value1")]
[InlineData("Value2")]
public Task UseTextForParameters(string arg)
{
    var settings = new VerifySettings();
    settings.UseTextForParameters(arg);
    return Verify(arg, settings);
}

[Theory]
[InlineData("Value1")]
[InlineData("Value2")]
public Task UseTextForParametersFluent(string arg)
{
    return Verify(arg)
        .UseTextForParameters(arg);
}

snippet source | anchor

Results in:

  • TheTest.UseTextForParameters_Value1.verified.txt
  • TheTest.UseTextForParameters_Value2.verified.txt
  • TheTest.UseTextForParametersFluent_Value1.verified.txt
  • TheTest.UseTextForParametersFluent_Value2.verified.txt