- Home
- .NET coding challenges
- Using Theory in xUnit for unit tests
Using Theory in xUnit for unit tests
xUnit is one of the unit test frameworks available for .NET Core applications.
When writing a test, you can use the Theory
attribute to create parameterised unit tests.
The parameters can be passed in using the InlineData
attribute.
Take this code:
public static class AdditionHelper
{
public static int Addition(int number1, int number2)
{
return number1 + number2;
}
}
public class AdditionTests
{
public void Addition_TwoNumbers_Equals9(int number1, int number2)
{
Assert.Equal(9, number1 + number2);
}
}
We are wanting to test the static Addition
method in the AdditionHelper
class. We want to add the Addition_TwoNumbers_Equals9
method as a test in our xUnit project.
However, this unit test will not run. What changes would we need to make for this test to run?
Give us your anonymous feedback regarding this page, or any other areas of the website.
Latest challenges
Convert hours, minutes and seconds into a time formatted string
Pass in the hours, minutes and seconds into a method and get it to return a time formatted string which should include leading zeros.
Contains online code editor
Work out the number of days between two dates
Write a C# function to work out the difference between two dates and return the number of days in this coding challenge.
Contains online code editor