Posts Tagged ‘TDD’

DataDriven Testing with Excel

Validating code with losts of scenario’s gets easier with validating the function with Excel. All you need is Excel.

Start of by naming the variables in the first row, one variable per column. Add an extra column for the result value (or more if needed). Fill out the variables and results.


[TestMethod]
[DeploymentItem("bonusCalculation.xlsx")]
[DataSource("System.Data.OleDb", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bonusCalculation.xlsx;Extended Properties=Excel 12.0;Persist Security Info=False", "bonus$", DataAccessMethod.Sequential)]
public void BonusCalcualtion()
{
    double result = Utils.GetSquareRoot(2);
    double omzet = (double)TestContext.DataRow ["Omzet"];
    double winst = (double)TestContext.DataRow["Winst"];
    double bonus = (double)TestContext.DataRow["Bonus"];

    Assert.AreEqual(bonus, Utils.BonusCalculation(omzet, winst));
}

When you do not have Office 2007, but need to run the test with the .xlsx you might want to download the data driver :
at Microsoft

If you prefer using an older fileformat of excel, compatible with MS Excel 2003 and 2000, use:

[TestMethod]
[DeploymentItem("bonusCalculation.xls")]
[DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='Calculator.xls';Persist Security Info=False;Extended Properties='Excel 8.0'", "bonus$", DataAccessMethod.Sequential)]
{
    //....
}