views:

33

answers:

2

Is it possible to get the values for a TestCaseAttribute from an external data source such as an Excel Spreadsheet, CSV file or Database? i.e. Have a .csv file with 1 row of data per test case and pass that data to NUnit one at a time.

Here's the specific situation that I'd like to use this for. I'm currently merging some features from one system into another. This is pretty much just a copy and paste process from the old system into the new one. Unfortunately, the code being moved not only does not have any tests, but is not written in a testable manner (i.e. tightly coupled with the database and other code.) Taking the time to make the code testable isn't really possible since its a big mess, i'm on a tight schedule and the entire feature is scheduled to be re-written from the ground up in the next 6-9 months. However, since I don't like the idea of not having any tests around the code, I'm going to create some simple Selenium tests using WebDriver to test the page through the UI. While this is not ideal, it's better than nothing.

The page in question has about 10 input values and about 20 values that I need to assert against after the calculations are completed, with about 30 valid combinations of values that I'd like to test. I already have the data in a spreadsheet so it'd be nice to simply be able to pull that out rather than having to re-type it all in Visual Studio.

A: 

You could always open your csv file in excel or any spreadsheet tool and then add a new column that concatenates the input/output values into the test case syntax.

Something like: =CONCATENATE("[TestCase(", A1, ",", B1, ",", C1, ",", D1, ",", E1, ")]")

Then copy/paste the column into the code.

Ezweb
A: 

I was finally able to accomplish what I wanted using NUnit's TestCaseSource attribute. The code looks a little ugly but it works.

Here is an example of pullind the data from a .csv file and passing it to the test method. The test is for the Add method of a simple calculator that takes two ints, adds them together and returns the sum.

Class to load the test data from the file.

public class TestData
{
    public int number1 { get; set; }
    public int number2 { get; set; }
    public int sum { get; set; }

    public static IEnumerable TestCases
    {
        get
        {
            string inputLine;
            using(FileStream inputStream = 
                new FileStream("C:\\Code\\TestData\\TestData.csv", 
                    FileMode.Open, 
                    FileAccess.Read))
            {
                StreamReader streamReader = new StreamReader(inputStream);

                while((inputLine = streamReader.ReadLine()) != null)
                {
                    var data = inputLine.Split(',');
                    yield return new TestData { 
                        number1 = Convert.ToInt32(data[0])
                        ,number2 = Convert.ToInt32(data[1])
                        ,sum = Convert.ToInt32(data[2]) 
                    };
                }

                streamReader.Close();
                inputStream.Close();
            }
        }
    }
}

Class with the actual tests:

[TestFixture]
public class CalculatorTests
{
    [Test]
    [TestCaseSource(typeof(TestData), "TestCases")]
    public void AddTwoNumbers(TestData data)
    {
        int sum = Calculator.Add(data.number1, data.number2);
        sum.ShouldEqual(data.sum);
    }
}

Contents of TestData.csv

4,4,8
15,20,35
8,8,16
5,5,10
42,13,55

It should be fairly simple to modify the get property in the TestData class to pull data from any datasource you want (i.e. Database, Web Service, Excel...)

Hamman359