0

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 PwmDataPoints.

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?

CHendrix
  • 378
  • 2
  • 12

1 Answers1

0

Figured it out. I started by changing the XML file to this:

<?xml version="1.0" encoding="utf-8" ?>
<TestData>
  <TestDatum>
    <PwmDataPoint IoState ="On" SecondsAtState="0.25"/>
    <TotalTime Time ="0.25"/>
  </TestDatum>
  <TestDatum>
    <PwmDataPoint IoState ="On" SecondsAtState="0.25"/>
    <PwmDataPoint IoState ="Off" SecondsAtState="0.25"/>
    <PwmDataPoint IoState ="On" SecondsAtState="0.25"/>
    <PwmDataPoint IoState ="Off" SecondsAtState="0.25"/>
    <PwmDataPoint IoState ="On" SecondsAtState="0.25"/>
    <TotalTime Time ="1.25"/>
  </TestDatum>
</TestData>

And then my test method works like this:

[TestMethod,
  DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
  "|DataDirectory|\\Data\\TotalTimeTestData.xml",
  "TestDatum",
  DataAccessMethod.Sequential)]
public void AddingNewDataPointUpdatesTotalTime()
{
  PwmData dataUnderTest = new PwmData();
  foreach(var row in TestContext.DataRow.GetChildRows("TestDatum_PwmDataPoint"))
  {
    IoState state = (IoState)Enum.Parse(typeof(IoState), row["IoState"].ToString());
    double timeAtState = Convert.ToDouble(row["SecondsAtState"].ToString());
    dataUnderTest.AddNewDataPoint(new PwmDataPoint(state, timeAtState));
  }
  double expected = Convert.ToDouble(TestContext.DataRow.GetChildRows("TestDatum_TotalTime")[0]["Time"]);

  Assert.AreEqual(expected, dataUnderTest.TotalSeconds);

}
CHendrix
  • 378
  • 2
  • 12