Tuesday, June 06, 2017

Test Driven Development Workshop

I recently led a Test Driven Development workshop using C# at work.  Here's the exercise I created to show how to do TDD.

 Introduction:

Red/Green Refactoring is starting with a failing test, and then making the target software work.

Remember to write the simplest thing that could possibly work

Ping-pong is having a pair of programmers work together.  The first programmer writes the test,  and the second makes the test pass

Workshop Background:
We just bought a zoo and employ jr. high students to write queries against our db for our web site.
The kids work hard, but their construction of URL strings to hit the web service is often bad.
We see final queries like
     https://WeBoughtAZoo.com/api&location=Asia
     https://WeBoughtAZoo.com/api&location=South America?type=reptile&pen=a2#4&time
        
    You need to write a helper URL builder function to make errors less likely, something like,
          
string url = Utilities.BuildUrl(host, name1, value1, name2, value2, ....)

Here's my example project. 
Clone solution and files from https://github.com/fincher42/TestDrivenDevelopmentWorkshop/tree/master

file ZooQuery.cs -- just a main program -- you will not change this file.
using System;

namespace TestDrivenDevelopment
{
    public class ZooQuery    {
       public static void Main(string[] args)
       {
           string host = "https://WeBoughtAZoo.com/api";
           string url;
            url = host + "?location=Africa";

          Console.In.ReadLine();
       }
    }
}

Utilities.cs -- this is where we write a function
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestDrivenDevelopment
{
    public static class Utilities
    {
               //this is where ReSharper will create the method BuildUrl
    }
}

UtilitiesTest.cs -- this is where we put the tests defined in the comments below
using System.Collections.Generic;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web;
using TestDrivenDevelopment;
using System;
using System.Linq;

namespace UnitTestProject1
{
    /*    Workshop tasks, Expected URLs:
     1. Simple name-value pair:
     BuildUrl(host, "location","Africa");
     2. Name-value pair with non-url friendly characters:
        BuildUrl(host, "animal pen","#16"); //(note: # in hex is 23)
     3. Multiple name-value pairs
        BuildUrl(host, "type","mammal","continent","Africa","size","small","food appetite","carnivore","animal pen","#16");
       code error conditions:
     4. Check for bad number of parameters
        BuildUrl(host, "type","mammal","continent");
     5. Check for bad host value, needs to look like a host name
    */
    [TestClass]
    public class UtilitiesTest
    {
     //your tests go here. 
    //Use Snippet to write the initial outline of the test

    }
}

 
Have one person create the test, and pass control to another person to upgrade BuildUrl() so it will pass the test. I have 5 exercises, so with 10 people everyone will get to code.

No comments: