This document discusses test driven development (TDD) and provides an overview and demonstration. It begins by covering software development, testing, and TDD. It then demonstrates TDD with a NodeJS example, showing how to start with a failing test and incrementally make it pass through small changes. The benefits of TDD are outlined as better designed code, understanding requirements, quality, and confidence to refactor. Challenges with TDD for microservices and frontends are discussed. It concludes with taking questions and providing further reading suggestions.
Test Driven Development: A Practitioner's Perspective
1. Test Driven Development
Malinda Kapuruge
@kaushalye
Guest lecture @ Swinburne University of Technology, Melbourne
a Practitioner’s Perspective
01 / 05 / 2019
9. Unit Tests
• Unit of code,
e.g., a function
• Smallest testable piece
• Assertions
• Data
• Quick to run (short feedback loop)
• Isolation (No connection to DBs or other services)
11. TDD != Writing unit tests
Application Tests
Tests Application
12. Write a test for
something that
doesn’t exist yet ?
13. Add test /
refactor
Fix
Run test
Fail
• Start with a failing test
• Make test pass
• Write more tests
• Make the pass … cont.
• Refactoring is part of the process
• Red-green-refactor
Pass
15. TDD Benefits
• Better designed code
• The interaction with other parts of code is already understood
• Better understood business requirements
• Tests are sort of a well-defined specification*
• Improved code quality
• Minimum code required to make the test pass
• Confidence to refactor code
• E.g., a programmer new to the team.
• Evolutionary technique
• Small steps towards end goal. Few lines of code at a time.
• Detect bugs early
• Assertions are already there
• Efficient and less costly
16. Feedback loop is minutes.
Source http://www.agilemodeling.com/essays/costOfChange.htm
18. Demo time!
• NodeJS – development platform
• https://nodejs.org/en/download/
• Jest – Test tool
• https://jestjs.io/
A function to check for strong passwords ...
19. What happened ?
• We did NOT start with the implementation
• We postponed answering the question “HOW?”
• We focused on expectations
• We clarified the requirements / specification
• We understood the inputs and outputs
• We started with the simplest form of the function, i.e., smallest step
• We observed failing tests
• We did small increments until the function is satisfactory
• We detect bugs early in the implementation
20. We ended up
with
A good test coverage
Minimal amount of code
Code satisfies current business
expectations / assertions
Assertions are captured as a test
specification
21. Tools that can help us
Test tools Development language / platform
JSUnit, Jasmine, Tape, Jest, Mocha, Chai, Sinon Node
Junit, Mockito, TestNG Java
PyUnit, Nose, pytest Python
Go test Go
Rspec, Test::Unit (builtin) Ruby
PHPUnit PHP
Cppunit, Google test, Boost C++
* These tools are OPTIONAL. But can make it easier to write and execute tests
22. Practical TDD
• TDD Zealots – do this or you are wrong
• Sometimes the first test doesn’t come naturally
• Playing with code and other components/services first
• Tests are good as long as all relevant assertions are covered.
• ATDD
• Passing tests vs Validness of solution (cheating)
• Bad unit tests -> bad TDD
• TDD - needs some practice
• Measure test coverage
24. Microservices
• Is an architecture style
• Loosely coupled services
• A service represents a specific business function
• Invoice service
• Customer service
• Payment service
• Separately deployed in different runtimes
• Highly maintainable, scalable and testable
25. runtime
Microservices and TDD
• Different runtimes and tech stacks
• Service – service interactions
• Mocks
• Version control
Runtime
Customer
Object
Invoice
Object
runtime
Customer
Service
Invoice
Service
26. Microservices and TDD
• Integration tests can be used to test the validity of service-service
interactions
• But TDD needs unit tests (short feedback loop)
27. Microservices and TDD
• Service contracts
• Pact framework
• A contract testing tool
• Consumer-driven
• Pact tests are unit tests
• TDD
Pact broker
Pact file
( version controlled )
Customer
Service
Invoice
Service
Unit
tests
Consumer
Provider
30. TDD for frontend
• Traditional frontend development
• Modern frameworks, come with all the goodies to start TDD
• e.g., react, angular,
• TDD for services and models
• E.g,, Enzyme
• TDD for UI/Visualization ???
• Can be done… but…
32. Behaviour-Driven Development
• Flavor that can be used to write unit tests
• Provide a structure
• Given, when, then format , e.g.,
Given('The user is authorised', function() {});
When('The user requests invoice data', function() {});
Then(‘Correct invoices are returned', function() {});
• Technical as well as non-technical users
• TDD and BDD complements each other
• Many tools, e.g., Cucumber, JBehave, RSpec.
33. Summary
• TDD is a software development methodology
• Start with failing tests, refactor until test pass.
Continue…
• Business requirements are captured as tests
• Many benefits
• Many tools
• Challenges in architectures like microservices
• To TDD or not to TDD? Case by case.