You Test? Do Test!

How and where testing fits into your busy dev life.

Confused
Andrew Furlan

What does it even mean to test?

A procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use.

Cupellation

Refining process in metallurgy.

The word "test" began to be used in the 17th century in place of "cupel."

Separate "noble" metals such as gold and silver from base metals such as lead and copper.

Testing is an act of refinement.

Un

Isolated. Fast.

Unit Testing

Tests a "unit" of code to ensure it does what is expected.

Narrow in scope.

No dependencies; mocked and stubbed.

In

Integrated. Fast.

Integration Testing

Tests multiple parts of an app working together.

Broad in scope

Many unit tests != an integration test.

UI

Integrated. Slower.

User Interface Testing

Focuses primarily on the front-end code.

Often conflated with e2e testing.

May include visual regression testing.

e2e

The whole enchilada. Slowest.

End-to-End Testing

Tests all parts of an app working together.

Test as the user could behave.

πŸ’ΈπŸ’ΈπŸ’ΈπŸ’ΈπŸ’ΈπŸ’ΈπŸ’ΈπŸ’ΈπŸ’ΈπŸ’Έ

Testing Pyramid
Inverted Testing Pyramid Ice Cream Cone
Inverted Testing Pyramid Ice Cream Cone Broken
Slow feedback loop diagram
Faster feedback loop

Chai.js

Assertion Library

  • expect('something').to.contain('thing');
  • expect('Hello World').to.be.a('string');
CHai.js Screenshot
var add = function (a, b) {
  return a + b;
};
var add = function (a, b) {
  return a + b;
};
expect(add(2, 2).to.equal(4));
var add = function (a, b) {
  return a + b;
};
expect(add(2, 2).to.equal(4));
expect(add(2, '2').to.not.equal('22'));
var add = function (a, b) {
  return a + b;
};
expect(add(2, 2).to.equal(4));
expect(add(2, '2').to.not.equal('22'));
expect(add('con','cat').to.not.equal('concat'));
var add = function (a, b) {
  return parseInt(a) + parseInt(b);
};
var add = function (a, b) {
  return parseInt(a) + parseInt(b);
};
expect(add(2, 2).to.equal(4));
expect(add(2, '2').to.not.equal('22'));
expect(add('con','cat').to.not.equal('concat'));
var add = function (a, b) {
  return parseInt(a) + parseInt(b);
};
expect(add(2, 2).to.equal(4));
expect(add(2, '2').to.not.equal('22'));
expect(add('con','cat').to.not.equal('concat'));
expect(add(2.22, 2.22).to.equal(4.44));
var add = function (a, b) {
  return parseInt(a) + parseInt(b);
};
expect(add(2, 2).to.equal(4));
expect(add(2, '2').to.not.equal('22'));
expect(add('con','cat').to.not.equal('concat'));
expect(add(2.22, 2.22).to.equal(4.44));

// AssertionError: expected 4 to equal 4.44

function numberToRomanNumeral(num) {
  var numeral = "";
  var arr = [
    [1000,"M"],[900,"CM"],[500,"D"],[400,"CD"],
    [100,"C"],[90,"XC"],[50,"L"],[40,"XL"],
    [10,"X"],[9,"IX"],[5,"V"],[4,"IV"],
    [1,"I"]
  ];
  arr.map(val => {
    while (num - val[0] >= 0) {
      num -= val[0];
      numeral += val[1];
    }
  })
  return numeral;
}
console.log(7, numberToRomanNumeral(7));
console.log(23, numberToRomanNumeral(23));
console.log(777, numberToRomanNumeral(777));
console.log(1000, numberToRomanNumeral(1000));
7 "VII"
23 "XXIII"
777 "DCCLXXVII"
1000 "M"

edit this code

See the Pen GEwKab by Branden Byers (@brndnb) on CodePen.

TDD

Test Driven Development


  • Red: Write a unit test that fails.
  • Green: Write just enough code to make the test pass.
  • Refactor: Clean up the code you just wrote.

See the Pen Mozgdm by Branden Byers (@brndnb) on CodePen.

See the Pen MozgRp by Branden Byers (@brndnb) on CodePen.

See the Pen NgEKmG by Branden Byers (@brndnb) on CodePen.

See the Pen PjxYxr by Branden Byers (@brndnb) on CodePen.

TDL

Test Driven Learning


  • Design: Design a code challenge/puzzle.
  • All Red: Write full test coverage.
  • All Green: Code your solution.
  • Break: Try to break your solution.
  • Refactor: Refactor your tests to cover any breakage.

Programmer Practice

  • Athletes Train

  • Musicians Rehearse

  • Doctors and Lawyers Practice

  • Crow
  • Parrot
Code Wars Screenshot
Dopamine Drip

Write Kickass Puzzles

Do it for yourself and for the world.


  • Step 1: Devise a description.
  • Step 2: Generate tests.
  • Step 3: Double, triple, and quadrupal check for edge cases.
  • Step 4: Solve and bathe in the dopamine.
  • Step 5: Share and repeat.

Test Like a User

Frustration Cat
Erik-Jan Leusink

Flaky Tests; Bad.

"With end-to-end tests, you have to wait: first for the entire product to be built, then for it to be deployed, and finally for all end-to-end tests to run. When the tests do run, flaky tests tend to be a fact of life. And even if a test finds a bug, that bug could be anywhere in the product."

β€” Mike Wacker, Software Engineer, Google

Bad tests are worse than no tests.

Who tests the tests?