際際滷

際際滷Share a Scribd company logo
Is your code ready for testing?
Ralph Ligtenberg
September 20, 2018
Ralph Ligtenberg
Tech Lead @ Travix
.Net/C#, Go, Google Cloud Platform
twitter.com/prutswonder
travix.io/@prutswonder
linkedin.com/in/ralphligtenberg
Who am I?
What is Test-Driven Development (TDD)?
A software development process that encourages developers to
write tests of their code before actually writing the code itself.
1. Write a test
2. Run tests to see it fail (RED)
3. Write just enough code to pass test
4. Run tests to see it succeed (GREEN)
5. REFACTOR code to make it right
6. Repeat!
How does TDD work?
(cue for demo)
Does TDD work for you?
Pros
 Makes you write code that you need
 Code coverage is high
 End solution is guaranteed to work
Cons
 Feels more like writing tests than writing code
 Easy to depart from the straightforward solution
 Can easily produce unmaintainable code
Does TDD work for you?
Write code first,
create tests later
What about an alternative?
 Make sure your code is SOLID
 Use pure functions
 Isolate code that use data sources
 Avoid unnecessary code: YAGNI
Ensure your code is testable
1. Write a piece of code your way
2. Write a test to cover a part of your code
3. Compromise your code
4. Run tests to see it fail (RED)
5. Restore your code
6. Run tests to see it succeed (GREEN)
7. Repeat (from step 2) until your code is covered
Test-ready development (TRD)
(another cue for demo)
Will TRD work for you?
Pros
 Focus is on code, not on writing tests
 Lets you write code your way
 Requires less refactoring
Cons
 Writing tests can be arbitrary
 Less effective if you dont know what youre doing
 Can lead to incorrect tests
Will TRD work for you?
TDD or TRD? Which one should I pick?
Exploratory code creation
 Use TDD
Fixed path code creation
 Use TRD
Thank you!

More Related Content

Is your code ready for testing?

  • 1. Is your code ready for testing? Ralph Ligtenberg September 20, 2018
  • 2. Ralph Ligtenberg Tech Lead @ Travix .Net/C#, Go, Google Cloud Platform twitter.com/prutswonder travix.io/@prutswonder linkedin.com/in/ralphligtenberg Who am I?
  • 3. What is Test-Driven Development (TDD)? A software development process that encourages developers to write tests of their code before actually writing the code itself.
  • 4. 1. Write a test 2. Run tests to see it fail (RED) 3. Write just enough code to pass test 4. Run tests to see it succeed (GREEN) 5. REFACTOR code to make it right 6. Repeat! How does TDD work?
  • 6. Does TDD work for you?
  • 7. Pros Makes you write code that you need Code coverage is high End solution is guaranteed to work Cons Feels more like writing tests than writing code Easy to depart from the straightforward solution Can easily produce unmaintainable code Does TDD work for you?
  • 8. Write code first, create tests later What about an alternative?
  • 9. Make sure your code is SOLID Use pure functions Isolate code that use data sources Avoid unnecessary code: YAGNI Ensure your code is testable
  • 10. 1. Write a piece of code your way 2. Write a test to cover a part of your code 3. Compromise your code 4. Run tests to see it fail (RED) 5. Restore your code 6. Run tests to see it succeed (GREEN) 7. Repeat (from step 2) until your code is covered Test-ready development (TRD)
  • 12. Will TRD work for you?
  • 13. Pros Focus is on code, not on writing tests Lets you write code your way Requires less refactoring Cons Writing tests can be arbitrary Less effective if you dont know what youre doing Can lead to incorrect tests Will TRD work for you?
  • 14. TDD or TRD? Which one should I pick? Exploratory code creation Use TDD Fixed path code creation Use TRD