© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
About me
Arnon Axelrod
TDD and Agile Quality ExpertE4D Solutions
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
About you
• Position: Dev, Test, Product, Management, other?
• TDD/Unit testing experience
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Why Learn TDD Today?
Adoption (% of martket)
time
Today
ATDD
TDD
Agile
Object-Oriented
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD?!
Photo by: Stuart Miles
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Learning TDD is like learning to ride a bicycle
© Arnon Axelrod, E4D Solutions Ltd.
Quality
Testing
Unit Tests
TDD
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
Quality throughout the project lifecycle
What is quality? Why it is important?
Quality
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Quality
No bugs
Stability
TestingDesign
Happy, Loyal customersUser eXperience
Clean code
Maintainability
Customer feedback
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
The Productivity Misconception
Productivity
Features
Happy customers
MaintainabilityPressure
Legend:Positive relationship
Negative relationship
Increased
Decreased
Productivity
Features
Happy customers
Stability
Clean code
Testing
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
The Productivity Misconception
Productivity
Features
Happy customers
MaintainabilityPressure
Legend:Positive relationship
Negative relationship
Increased
Decreased
Stability
Clean code
Testing
© Arnon Axelrod, E4D Solutions Ltd.
Quality
Testing
Unit Tests
TDD
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
TestingTesting
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Types of Tests
ExploratoryPlanned
ManualAutomated
White box Black box
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Types of Tests
End-to-End
UIView ModelClient Logic
Server Proxy
Service LayerBusiness Logic
DAL
ORM
DB
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Types of TestsUI
View ModelClient Logic
Server Proxy
Service LayerBusiness Logic
DAL
ORM
DB
Usability, UX
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Types of TestsUI
View ModelClient Logic
Server Proxy
Service LayerBusiness Logic
DAL
ORM
DB
Integration
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Types of TestsUI
View ModelClient Logic
Server Proxy
Service LayerBusiness Logic
DAL
ORM
DB
Functional
Mock
Mock
Mock
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Types of TestsUI
View ModelClient Logic
Server Proxy
Service Layer
DAL
ORM
DB
Unit tests
Business Logic
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Types of tests
• Other:
– Performance
– Load (scalability)
– Install (deployment)
– Monkey testing
© Arnon Axelrod, E4D Solutions Ltd.
Quality
Testing
Unit Tests
TDD
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
Unit tests basicsUnit Tests
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
What’s a unit test?
• What’s a unit?
– Smallest testable functionality
– any testable functionality (BDD)
• Automatic
• Gray box
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Benefits of Unit Tests
• Fast
• Easy to write and maintain
• Code coverage
• When fail, provide insight into the problem
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Unit tests should:• Be Isolated– Re-runnable
• No side effects• Cleanup
– Environment agnostic– Order doesn’t matter
• Verify functionality, not implementation
• Be straight-forward– No conditionals, loops etc
Photo by: tongdang
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Unit tests should:
• Read like a story
• Verify only one thing
• Have meaningful names
• Be fast!
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Inventory class+int GetAvailablePieces (Item item)
+void Reduce(Item item, int count)
- DbConnectionProvider
Doubles (mocks)
Order class+Items
+bool CheckAvailability (IInventory inventory)
+void Supply (IInventory inventory)
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Doubles (mocks)
Order class+Items
+bool CheckAvailability (IInventory inventory)
+void Supply (IInventory inventory)
IInventory interface+int GetAvailablePieces (Item item)
+void Reduce(Item item, int count)
InventoryDouble+int GetAvailablePieces (Item item)
+void Reduce(Item item, int count)
Inventory class+int GetAvailablePieces (Item item)
+void Reduce(Item item, int count)
- DbConnectionProvider
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Doubles...
Double
Fake
Dummy MockStub
Detour
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Unit test structureArrange
Act
Assert
Given
When
Then
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Test suiteSuite Initialize
Test Initialize
Test Method 1
Test Cleanup
Suite Cleanup
Test Method 2 Test Method 3
© Arnon Axelrod, E4D Solutions Ltd.
Quality
Testing
Unit Tests
TDD
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
TDD
TDD
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
What TDD is all about?
Testing?!
Design!
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Why it is important to test First?• Looking at the problem space
– vs. the solution (implementation) space
• Building decoupled code from base
• Psychological effect
• Testing the test first!
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Testing the Test First!
• What are the inputs?• What are the outputs?
[TestMethod]public void TestFibonacciFunction(){ // arrange ... // Act: var result = Fibonacci(...); // Assert: Assert ...}
The test we want to test
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Testing the Test First!public bool TestFibonacciFunction(Func<…> fibonacci){ // arrange ... // Act: var result = fibonacci(...); // Assert: return ...}
The test we want to test
public void TestTheTest(){ var invalidImpl = … var goodImpl = Fibonacci; Assert.IsFalse(TestFibonacciFunction(invalidImpl)); Assert.IsTrue(TestFibonacciFunction(goodImpl));}
Testing the test
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
The way TDD works:
Write a failing test
Write the code to
make this test pass
Refactor
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Write a test
New Test
Old Tests
Write production
codeAll
Tests
Refactor
All Tests
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD and Refactoring
Photo by: Chaiwat
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD and Refactoringpublic class SomeClass { public SomeClass() { //... } public void Foo(int someParam) { // use someParam... }}
Initial state
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD and Refactoringpublic class SomeClass { private int _someParam; public SomeClass(int someParam) { //... _someParam = someParam; } public void Foo() { // use _someParam... }}
Final (desired) state
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD and Refactoringpublic class SomeClass { public SomeClass() { //... } public void Foo(int someParam) { // use someParam... }}
Initial state
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD and Refactoringpublic class SomeClass{ public SomeClass() { //... } private int _someParam; public SomeClass(int someParam) : this() { _someParam = someParam; } public void Foo(int someParam) { //use someParam... }}
Step1 – Add constructor
overload
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD and Refactoringpublic class SomeClass{ private int _someParam; public SomeClass(int someParam) { //... _someParam = someParam; } public void Foo(int someParam) { //use someParam... }}
Step2 – Remove old constructor
overload
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD and Refactoringpublic class SomeClass{ private int _someParam; public SomeClass(int someParam) { //... _someParam = someParam; } public void Foo(int someParam) { //use someParam... } public void Foo() { //use _someParam... }}
Step3 – Add new overload of
Foo
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
TDD and Refactoringpublic class SomeClass{ private int _someParam; public SomeClass(int someParam) { //... _someParam = someParam; } public void Foo() { //use _someParam... }}
Step4 (final) – Remove old
overload of Foo
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Limits of TDD• UI
• Multi-threading
• Integration with peripheral systems, including:– Hardware (I/O)– Operating System– Database– 3rd party applications and components
© Arnon Axelrod, E4D Solutions Ltd.
Quality
Testing
Unit Tests
TDD
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
ATDD
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
ATDD
Stands for:
Accepance-Test Driven Development
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
ATDD
TDD is about writing the CODE RIGHT
ATDD is about writing the RIGHT CODE
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
ATDD
ATDD is about Communication
Mainly between Product, Dev and Test
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
ATDD is AKA:
• Agile Acceptance Testing• Specification by Example• Example Driven Development• Executable Specifications• ~= BDD (Behavior Driven Development)
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Problems with Waterfall Specification
Photo by: Michal Marcol
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Problems with Agile Specification
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
How ATDD works?
Specification by
ExamplesScenarios Acceptance
tests
© Arnon Axelrod, E4D Solutions Ltd.
Quality
Testing
Unit Tests
TDD
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
FitNesse Demo
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Benefits of ATDD for the Business Analyst• Developers will actually read the specifications that you write• You will be sure that developers and testers understand the
specifications correctly• You will be sure that they do not skip parts of the specification• You can track development progress easily• You can easily identify conflicts in business rules and
requirements caused by later change requests• You’ll save time on acceptance and smoke testing
Source: Gojko Adzic – Bridging the Communication Gap
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Benefits of ATDD for the Developer
• Most functional gaps and inconsistencies in the requirements and specifications will be flushed out before the development starts
• You will be sure that business analysts actually understand special cases that you want to discuss with them
Source: Gojko Adzic – Bridging the Communication Gap
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Benefits of ATDD for the Developer
• You will have automated tests as targets to help you focus the development.
• It will be easier to share, hand over and take over code
• You’ll have a safety net for refactoring, making your code cleaner [Arnon A.]
Source: Gojko Adzic – Bridging the Communication Gap
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Benefits of ATDD for the Tester• You can influence the development process and stop
developers from making the same mistakes over and over• You will have a much better understanding of the domain• You’ll delegate a lot of dull work to developers, who will
collaborate with you on automating the verifications
Source: Gojko Adzic – Bridging the Communication Gap
© Arnon Axelrod, E4D Solutions Ltd.
© Arnon Axelrod, E4D Solutions Ltd.
Benefits of ATDD for the Tester• You can build in quality from the start by raising concerns
about possible problems before the development starts• You’ll be able to verify business rules with a touch of a button• You will be able to build better relationships with developers
and business people and get their respect• You’ll spend less time doing dull work, and more time
exploring “real” bugs and testing non-functional aspects (e.g. performance, usability, etc). [Arnon A.]
Source: Gojko Adzic – Bridging the Communication Gap
© Arnon Axelrod, E4D Solutions Ltd.
Quality
Testing
Unit Tests
TDD
ATDD
© Arnon Axelrod, E4D Solutions Ltd.
Questions?