why xunit not allow test a method with parameters?

Cheung picture Cheung · Nov 17, 2011 · Viewed 8.5k times · Source

I am learning to use unit test, i create a project, add xunit reference. And following codes:

namespace UnitTestProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [Fact]
        private void test(int number1, string number2)
        {

            int result = number1 + Convert.ToInt32(number2);
            Assert.IsType(Type.GetType("Int32"), result);
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

When i run the test using xunit gui tool, it said:

UnitTestProject.Form1.test : System.InvalidOperationException : Fact method UnitTestProject.Form1.test cannot have parameters Stack Trace: 於 Xunit.Sdk.FactCommand.Execute(Object testClass)
Xunit.Sdk.FixtureCommand.Execute(Object testClass)
Xunit.Sdk.BeforeAfterCommand.Execute(Object testClass)
Xunit.Sdk.LifetimeCommand.Execute(Object testClass)
Xunit.Sdk.ExceptionAndOutputCaptureCommand.Execute(Object testClass)

So, how can i test the method/function with parameters?

Answer

Alina picture Alina · Nov 17, 2011

Also you can use [Theory] instead of [Fact]. It will allow you to create test methods with different parameters. E.g.

[Theory]
[InlineData(1, "22")]
[InlineData(-1, "23")]
[InlineData(0, "-25")]
public void test(int number1, string number2)
{
    int result = number1 + Convert.ToInt32(number2);
    Assert.IsType(Type.GetType("Int32"), result);
}

p.s. With xUnit it would be better to make test methods public.