- 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?
Latest challenges
![Work out the number of days between two dates](/media/uzyksmqi/codingchallengedaystogo.png?width=350&height=197&v=1da1a4cba8891c0)
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
![Removing the last character from a string](/media/av2lvfyl/codingchallengelastcharacter.png?width=350&height=197&v=1da14a72f0f3270)
Removing the last character from a string
This coding challenge involves writing a C# method that will removing the last character from a string and returning the value.
Contains online code editor