Edit on GitHub

xUnit1026 Warning

Theory methods should use all of their parameters

Cause

This rule is triggered by having an unused parameter on your [Theory].

Reason for rule

Unused parameters are typically an indication of some kind of coding error (typically a parameter that was previously used and is no longer required, or a parameter that was overlooked in the test implementation).

As of xunit.analyzers version 1.2, this rule will no longer trigger with parameters that are named as discards (meaning, starting with _ and optionally 1 or more numbers). For more information on discard parameter names, see IDE0060.

How to fix violations

To fix a violation of this rule, you may:

Examples

Violates

using Xunit;

static class MyGreetingService
{
    public static string Greet(string name) =>
        $"Hello, {name}!";
}

public class xUnit1026
{
    [Theory]
    [InlineData("Joe", 42)]
    public void ValidateGreeting(string name, int age)
    {
        var result = MyGreetingService.Greet(name);

        Assert.Equal("Hello, Joe!", result);
    }
}

Does not violate

using Xunit;

static class MyGreetingService
{
    public static string Greet(string name) =>
        $"Hello, {name}!";
}

public class xUnit1026
{
    [Theory]
    [InlineData("Joe", 42)]
    public void ValidateGreeting(string name)
    {
        var result = MyGreetingService.Greet(name);

        Assert.Equal("Hello, Joe!", result);
    }
}
using Xunit;

static class MyGreetingService
{
    public static string Greet(string name) =>
        $"Hello, {name}!";
}

public class xUnit1026
{
    [Theory]
    [InlineData("Joe", 42)]
    public void ValidateGreeting(string name, int _)
    {
        var result = MyGreetingService.Greet(name);

        Assert.Equal("Hello, Joe!", result);
    }
}
using Xunit;

static class MyGreetingService
{
    public static string Greet(string name, int age) =>
        $"Hello, {name}! How do you like being {age} years old?";
}

public class xUnit1026
{
    [Theory]
    [InlineData("Joe", 42)]
    public void ValidateGreeting(string name, int age)
    {
        var result = MyGreetingService.Greet(name, age);

        Assert.Equal("Hello, Joe! How do you like being 42 years old?", result);
    }
}
Copyright © .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit/tree/gh-pages.