I'm trying to create a data driven test that calls a method multiple times with different input on each call. In this particular case, the test is verifying a class maintains a running sum of the total time.
Here's the code under test:
public Enum IoState{On, Off}
public class PwmDataPoint
{
public IoState LineState { get; private set; }
public double SecondsAtState { get; private set; }
public PwmDataPoint(IoState lineState, double secondsAtState)
{
this.LineState = lineState;
this.SecondsAtState = secondsAtState;
}
}
public class PwmData
{
public double TotalSeconds {get; private set;} = 0;
public void AddNewDataPoint(PwmDataPoint addMe)
{
this.TotalSeconds+= addMe.SecondsAtState;
//Other data processing code that other tests will cover...
}
}
The test I'm trying to create will verify that PwmData.TotalSeconds
gets updated correctly when PwmData.AddNewDataPoint
is called.
Obviously, PwmData.AddNewDataPoint
can be called any number of times during the lifetime of a PwmData
object.
I'm trying to write a data-driven test in MSTest, and I can't figure out how to load a random number of data points out of my XML test data file.
Here's a snippit from the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<TestData>
<TestDatum>
<PwmDataPoint>
<IoState>On</IoState>
<SecondsAtState>0.25</SecondsAtState>
</PwmDataPoint>
<TotalTime>0.25</TotalTime>
</TestDatum>
<TestDatum>
<PwmDataPoint>
<IoState>On</IoState>
<SecondsAtState>0.25</SecondsAtState>
</PwmDataPoint>
<PwmDataPoint>
<IoState>On</IoState>
<SecondsAtState>0.25</SecondsAtState>
</PwmDataPoint>
<PwmDataPoint>
<IoState>Off</IoState>
<SecondsAtState>0.25</SecondsAtState>
</PwmDataPoint>
<PwmDataPoint>
<IoState>On</IoState>
<SecondsAtState>0.25</SecondsAtState>
</PwmDataPoint>
<PwmDataPoint>
<IoState>Off</IoState>
<SecondsAtState>0.25</SecondsAtState>
</PwmDataPoint>
<TotalTime>1.25</TotalTime>
</TestDatum>
</TestData>
Each TestDatum
is the data needed to run a test. In this case, there's 2 tests to run: The first test has 1 PwmDataPoint
to use, where the second test will use 5 PwmDataPoint
s.
I'm fairly new to data driven MSTests, and all of the examples I could find have a constant number of input data points.
How can I dynamically load N number of input data points in MSTest from an XML File?