TDD

TDD,

TOC

Development of individual functions. Use natural language to create examples. To make the examples executable:

Resources

Java

JUnit5

Team, Github, Tutorial, Github

Tutorial

src
    main
    test
        java
        resources
            com
                quanpan302
                    tdd
                        "GameApplicationTests.java"
// GameApplicationTests.java
import static org.junit.jupiter.api.Assertions.assertEquals;

// [Annotations](https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations)
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;

public class GameApplicationTests {
     
    @BeforeAll
    static void setup(){
        System.out.println("@BeforeAll executed");
    }
     
    @BeforeEach
    void setupThis(){
        System.out.println("@BeforeEach executed");
    }
     
    @Test
    void testCalcOne() 
    {
        System.out.println("======TEST ONE EXECUTED=======");
        assertEquals( 4 , Calculator.add(2, 2));
    }
     
    @Test
    void testCalcTwo() 
    {
        System.out.println("======TEST TWO EXECUTED=======");
        assertEquals( 6 , Calculator.add(2, 4));
    }
     
    @AfterEach
    void tearThis(){
        System.out.println("@AfterEach executed");
    }
     
    @AfterAll
    static void tear(){
        System.out.println("@AfterAll executed");
    }
}

Console output

@BeforeAll executed

@BeforeEach executed
======TEST ONE EXECUTED=======
@AfterEach executed

@BeforeEach executed
======TEST TWO EXECUTED=======
@AfterEach executed

@AfterAll executed

Javascript

Jest, Facebook

Team, Github

// sum.js
function sum(a, b) {
  return a + b;
}
module.exports = sum;

// sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

Karma, Angular

Team, Github

// calculator.js
'use strict';

window.calculator = window.calculator || {};

(function() {
  var getIntById = function(id) {
    return parseInt(document.getElementById(id).value, 10);
  };

  var calculate = function() {
    var sum = getIntById('x') + getIntById('y');
    document.getElementById('result').innerHTML = isNaN(sum) ? 0 : sum;
  };

  window.calculator.init = function() {
    document.getElementById('add').addEventListener('click', calculate);
  };
})();

// calculator.test.js
/*
 * Unit tests for lib/calculator.js
 */

describe('Calculator', function() {

  // inject the HTML fixture for the tests
  beforeEach(function() {
    var fixture = '<div id="fixture"><input id="x" type="text">' + 
      '<input id="y" type="text">' + 
      '<input id="add" type="button" value="Add Numbers">' +
      'Result: <span id="result" /></div>';

    document.body.insertAdjacentHTML(
      'afterbegin', 
      fixture);
  });

  // remove the html fixture from the DOM
  afterEach(function() {
    document.body.removeChild(document.getElementById('fixture'));
  });

  // call the init function of calculator to register DOM elements
  beforeEach(function() {
    window.calculator.init();
  });

  it('should return 3 for 1 + 2', function() {
    document.getElementById('x').value = 1;
    document.getElementById('y').value = 2;
    document.getElementById('add').click();
    expect(document.getElementById('result').innerHTML).toBe('3');
  });

  it('should calculate zero for invalid x value', function() {
    document.getElementById('x').value = 'hello';
    document.getElementById('y').value = 2;
    document.getElementById('add').click();
    expect(document.getElementById('result').innerHTML).toBe('0');
  });

  it('should calculate zero for invalid y value', function() {
    document.getElementById('x').value = 1;
    document.getElementById('y').value = 'goodbye';
    document.getElementById('add').click();
    expect(document.getElementById('result').innerHTML).toBe('0');
  });

});

Protractor, Angular

Team, Github

// src/first_spec.js
describe('angularjs homepage', function() {
  it('should have a title', function() {
    browser.get('http://angularjs.org/');
    expect(browser.getTitle()).toContain('AngularJS');
  });
});

// config/first_spec.js
exports.config = {
// An example configuration file.
exports.config = {
  directConnect: true,

  // Capabilities to be passed to the webdriver instance.
  capabilities: {
  'browserName': 'chrome',
},

  // Framework to use. Jasmine is recommended.
  framework: 'jasmine',

  // Spec patterns are relative to the current working directory when
  // protractor is called.
  specs: ['first_spec.js'],
};
}

Selenium, automates browsers

Team, Github

Javascript

Python