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?
Thereby, the xUnit test eliminates the risk of test method dependencies.
Fact vs Theory Tests The primary difference between fact and theory tests in xUnit is whether the test has any parameters. Theory tests take multiple different inputs and hold true for a particular set of data, whereas a Fact is always true, and tests invariant conditions.
The MSBuild runner in xUnit.net v2 is capable of running unit tests from both xUnit.net v1 and v2. It can run multiple assemblies at the same time, and build file options can be used to configuration the parallelism options used when running the tests.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With