I am looking to use MS testing framework to test a tax calculator and needs to run over 100 test cases. As see below, I have set up an array of testCases and loop through each but the when I run Tests, it only returns one return.
Question:
[DataTestMethod]
public void CalculateResult(int val)
{
// arrange
int[] testCase = { 1000,2000, 35000, 400003, 59509 };
foreach (int income in testCase)
{
double expectOutput = TaxCalculator(income);
// act
SUT.GeneralTaxRule generalTaxationRule = new SUT.GeneralTaxRule(income);
double actualOutput = generalTaxationRule.getTax();
// assert
Assert.AreEqual(expectOutput, actualOutput);
Console.WriteLine(expectOutput, actualOutput);
}
}
When you use DataTestMethod
attribute you should have attributes for each DataRow
[DataTestMethod]
[DataRow(1000)]
[DataRow(2000)]
// etc
public void CalculateResult(int income)
{
double expectOutput = TaxCalculator(income);
// act
SUT.GeneralTaxRule generalTaxationRule = new SUT.GeneralTaxRule(income);
double actualOutput = generalTaxationRule.getTax();
// assert
Assert.AreEqual(expectOutput, actualOutput);
Console.WriteLine(expectOutput, actualOutput);
}
Another way you might achieve the same is using DynamicData
attribute
[DataTestMethod]
[DynamicData(nameof(TestData), DynamicDataSourceType.Method)]
public void CalculateResult(int income)
{
double expectOutput = TaxCalculator(income);
// act
SUT.GeneralTaxRule generalTaxationRule = new SUT.GeneralTaxRule(income);
double actualOutput = generalTaxationRule.getTax();
// assert
Assert.AreEqual(expectOutput, actualOutput);
Console.WriteLine(expectOutput, actualOutput);
}
public static IEnumerable<object[]> TestData()
{
yield return new object[] { 1000 };
yield return new object[] { 2000 };
/// etc.
}
You could data drive the data however you like in TestData
above (eg a file, a database, or hardcoded). There is other options for DynamicDataSourceType
as well which you could investigate.
Using Xunit, you could do it like this:
private static int[] incomesArray = { 1000,2000, 35000, 400003, 59509 };
public static IEnumerable<object[]> GetIncomeTestData =>
incomesArray
.Select(x => new object[] { x })
.ToArray();
[Theory]
[MemberData(nameof(GetIncomeTestData))]
public void CalculateResult(int income)
{
// arrange
var expectOutput = TaxCalculator(income);
var generalTaxationRule = new SUT.GeneralTaxRule(income);
// act
var actualOutput = generalTaxationRule.getTax();
// assert
Assert.Equal(expectOutput, actualOutput);
}
You can have as many values in incomesArray
as you'd like and you'll be able to see the result for each test value.
If you're using a [DataTestMethod]
you can add test cases through one or more [DataRow(...)]
attribute(s):
[DataTestMethod]
[DataRow(1000)]
[DataRow(2000)]
[DataRow(35000)]
[DataRow(400003)]
[DataRow(59509)]
public void CalculateResult(int val)
{
double expectOutput = TaxCalculator(val);
// act
SUT.GeneralTaxRule generalTaxationRule = new SUT.GeneralTaxRule(val);
double actualOutput = generalTaxationRule.getTax();
// assert
Assert.AreEqual(expectOutput, actualOutput);
Console.WriteLine(expectOutput, actualOutput);
}
That way you can inspect the test result for each val
in the Test Explorer window.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.