SDD

University of London CM 2010 Software Design and Development


Project maintained by ccy05327 Hosted on GitHub Pages — Theme by mattgraham

Topic 2 Test-Driven Development

Week 5 Test-Driven development

Reading

Basics

Three laws of unit-testing:

The following special issue on test-driven development contains several interesting articles:

(PDF) Segura, S. and Z.Q. Zhou ‘Metamorphic testing 20 years later: a hands-on introduction’, 2018 IEEE/ACM 40th International Conference on Software Engineering: Companion (ICSE-Companion) 2018, pp.538–539.

(PDF) Borle, N., M. Feghhi, E. Stroulia, R. Grenier and A. Hindle ‘[Journal First] Analyzing the effects of test driven development in GitHub’, 2018 IEEE/ACM 40th International Conference on Software Engineering (ICSE) 2018, pp.1062–1062.

Learning Objectives

Three Laws of TDD

Demo of unit test workflow

Requirement

  1. Test: does the getHighest function return a value?

    1. Write a failed test.

      function testReturnVal() {
          let input = [1, 2, 4, 5, 6];
          let result = getHighest(input);
      
          return !(result == undefined);
      }
      
      let result1 = testReturnVal();
      if (result1) console.log("testReturnVal passed");
      else console.log("testReturnVal failed");
      
      // ReferenceError: getHighest is not defined
      
    2. Write the minimum code to pass the test.

      function getHighest(input) {
          return 0;
      }
      
      // testReturnVal passed
      
  2. Test: does it return a value from the array?

    function testResultInArray() {
    let input = [1, 2, 3];
    let result = getHighest(input);
    
    return result == 1 || result == 2 || result == 3;
    }
    
    let result2 = testResultInArray();
    if (result2) console.log("testResultInArray passed");
    else console.log("testResultInArray failed");
    
    // testReturnVal passed
    // testResultInArray failed
    

    Change getHighest() to:

    function getHighest(input) {
        return input[0];
    }
    
    // testReturnVal passed
    // testResultInArray passed
    
  3. Test: does it return the correct array/value?

    function testCorrectResult() {
        let input = [1, 2, 3];
        let result = getHighest(input);
        return result == 3;
    }
    
    ...
    
    let result3 = testCorrectResult();
    if (result3) console.log("testCorrectResult passed");
    else console.log("testCorrectResult failed");
    
    // testReturnVal passed
    // testResultInArray passed
    // testCorrectResult failed
    

    Change getHighest() to:

    function getHighest(input) {
    
        let max = input[0];
        for (let i = 0; i < input.length; i++) {
            if (input[i] > max) max = input[i];
        }
        return max;
    }
    
    // testReturnVal passed
    // testResultInArray passed
    // testCorrectResult passed
    

    Full test file in JavaScript

What to test?

Week 6 Unit testing in Python

Learning Objectives

Reading

Unittest in Python

import unittest

class TestSetForOneModule(unittest.TestCase):
    def test_arithmetic_pass(self):
        self.assertEqual(2+2, 4)

    def test_arithmetic_fail(self):
        self.assertEqual(2+2, 3)

unittest.main(argv=['ignored', '-v'], exit=False)

Week 7 Unit testing in C++

Learning Objectives

Reading

cppunit in C++

Week 8 Unit testing a web API

Learning Objectives

This week we will be working with various tools. You can use our browser-based JavaScript environment to carry out the work – in that case, you should not need to install anything.

If you plan to work on your own machine, you will be given instructions on which tools to install later, but you can get start now by installing Node.js. We recommend version 12 LTS.

We will be using Mocha and Chai to carry out the unit testing.

Might need Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass for PowerShell to run the tests.