I'm fairly new to Unit Testing and have the following code:
public class PowerOf
{
public int CalcPowerOf(int @base, int exponent) {
if (@base == 0) { return 0; }
if (exponent == 0) { return 1; }
return @base * CalcPowerOf(@base, exponent - 1);
}
}
The unit test (with xUnit) I wrote for it first was this one, but I'm not quite sure if it's the right approach, or if I should use another pattern? What I wanted to know is whether this is the correct usage for passing multiple sets of data into a "unit test" - as I didn't see any docs or reference examples on xUnit's docs?
[Fact]
public void PowerOfTest() {
foreach(var td in PowerOfTestData()) {
Assert.Equal(expected, CalcPowerOf(@base, exponent));
}
}
public class TestData {
int Base {get;set;}
int Exponent {get;set;}
int ExpectedResult {get;set;}
}
public List<TestData> PowerOfTestData() {
yield return new TestData { Base = 0, Exponent = 0, TestData = 0 };
yield return new TestData { Base = 0, Exponent = 1, TestData = 0 };
yield return new TestData { Base = 2, Exponent = 0, TestData = 1 };
yield return new TestData { Base = 2, Exponent = 1, TestData = 2 };
yield return new TestData { Base = 5, Exponent = 2, TestData = 25 };
}