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?
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.