Data driven tests using Google Spreadsheets

I’m using Google Docs to share test data. That way test data (accounts, purchases etc.) can be set up in advance and test execution can be done using the pre-created data. It saves a lot of time and streamlines the testing effort.

When executing a test, a tester can just go to the google doc and get a test account already configured with the proper data for the scenario they are testing. This can also extend to automation with one extra step.

The test automation I’ve written in C# originally was written for a single scenario. After some refactoring, I had data driven test cases in NUnit. This is simple to accomplish with an annotation. The original test might look something like this:

using System;
using NUnit.Framework;

[TestFixture]
namespace oneshore.qa.testfixtures
{
    public class MyTest
    {
        [TestCase]
        public void testSanity()
        {
            Assert.IsTrue(Sky.color == Colors.BLUE);
        }
    }
}

By adding parameters to the [TestCase] attribute we can come up with a (slightly) data driven test:

        [TestCase(Color.BLUE, Result=true)]
        [TestCase(Color.GREEN, Result=false)]
        [TestCase(Color.RED, Result = false)]
        public bool testSanity(Color expectedColor)
        {
            return Sky.color == expectedColor;
        }

You can also specify a data source method with the TestCaseSource attribute:

       [TestCaseSource("MyDataMethod")]
        public void testInsanity(Color unexpectedColor)
        {
            if (unexpectedColor != Color.BLUE)
                Assert.IsFalse(Sky.color == unexpectedColor);
        }

        private IEnumerable<Color> MyDataMethod()
        {
            List<Color> colors = new List<Color>{Color.BLUE, Color.GREEN, Color.RED};
            
            foreach (Color color in colors)
                yield return color;
        }

It’s now a fairly simple exercise to get your test data from a file. And just one more step to use the Google Data API to get it from a spreadsheet. I’ll detail that next.

Leave a comment