Having an actual decimal value as parameter for an attribute (example xUnit.net's [InlineData]
You should be able use the String value in the Attribute and set the Parameter type to Decimal, it get's converted automatically by the Test Framework as far as I can tell.
[Theory]
[InlineData("37.60")]
public void MyDecimalTest(Decimal number)
{
Assert.Equal(number, 37.60M);
}
If this doesn't work then you can manually convert it by passing in a String parameter.
[Theory]
[InlineData("37.60")]
public void MyDecimalTest(String number)
{
var d = Convert.ToDecimal(number);
Assert.Equal(d, 37.60M);
}
Instead of InlineData
, use MemberData
as shown here. This gives you much greater flexibility in setting up multiple tests and allows the use of decimals or any other non-constant type.
public class CalculatorTests
{
public static IEnumerable<object[]> Data =>
new List<object[]>
{
new object[] { 1.2M, 2.1M, 3.3M },
new object[] { -4.000M, -6.123M, -10.123M }
};
[Theory]
[MemberData(nameof(Data))]
public void CanAddTheoryMemberDataProperty(decimal value1, decimal value2, decimal expected)
{
var calculator = new Calculator();
var result = calculator.Add(value1, value2);
Assert.Equal(expected, result);
}
}