0

I have a data conversion function I want to unit test. I have Identified a finite but rather long list of possible inputs (around 1000) that can lead to about 50 outputs. Is there a way in VS unit testing to set up a list of inputs and a list of expected outputs (like in a two column Excel chart) and feed that into the unit test?

If there is a way, how? Thanks.

Daniel
  • 1,391
  • 2
  • 19
  • 40
  • It looks like you want seomthing like this: http://stackoverflow.com/questions/2367033/mstest-equivalent-for-nunits-parameterized-tests – Justin Pihony May 14 '12 at 16:21

2 Answers2

1

MSTest has DataSourceAttribute. By marking unit test method with it you grant your test access to, well - data source. It supports many various formats, from databases to CSV or excel files.

Example from this blog post:

[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
    "MyWidgetTests.csv", "MyWidgetTests#csv", DataAccessMethod.Sequential)]
public void TestMyBusinessLogicWithCsv()
{
    int valueA = Convert.ToInt32(TestContext.DataRow["valueA"]);
    int valueB = Convert.ToInt32(TestContext.DataRow["valueB"]);
    int expectedResult = Convert.ToInt32(TestContext.DataRow["expectedResult"]);
    int actualResult = MyWidget.MyBusinessLogic(valueA, valueB);
    Assert.AreEqual(expectedResult, actualResult, 
        "The result returned from the widget was not as expected.");
}
k.m
  • 30,794
  • 10
  • 62
  • 86
0

If you have that many inputs, you can write this as an automated integration test. Specifically, list all the input/outputs in some file, read that file in your test, and execute the tests this way.

This keeps your test simple and easy to read, and all of the data is external and can still be easily changed without confusing the logic of the actual test.

Oleksi
  • 12,947
  • 4
  • 56
  • 80