Test driven development

Article posted on and takes 4 min. to read.

What is TDD

Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed.

Benefits of TDD

TDD saves 40%-80% bugs.

Due to exponentially escalating costs of rework, meta-work (work about work), responsibility hand-offs, interruptions, and eventually customer support, bug triage, and maintenance, fixing a production bug may cost 100x more than fixing a bug at design time, and over 15x more than fixing a bug at implementation time.

Supposed drawbacks of TDD

How to TDD

  1. Write a good unit test - a unit test should take into account all input, output and possible exceptions of a tested code.
    • AAA principle:
      • Arrange - variables, fields, properties, exceptions;
      • Act - call a method with different input variations;
      • Assert - verify result and expected exceptions;
    • Test limitations:
      • What happens when input/output is different type (bool/int/string)?
      • What happens when expected output is an emoji?
      • What happens when expected output is 3000 entries?
      • What happens when method is interrupted?
      • What happens when method times out?
      • What must happen for method to run out of memory?
    • Tests should be independent:
      • Tested function should not involve other tested functions;
      • Unit tests should not depend on previous test states (setup/teardown discipline);
  2. Run the accepted tests - expect a failure;
  3. (Re) Write function - make changes for test to pass;
  4. Back to point #2

How not to TDD

const tests = [
	'applyFee' = [
		'input' = 11,
		'expectedOutput' = 233
		'error' = 'Fee has not been applied'
	]
]

TDD Examples