Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2014 Graduate Bootcamp: TDD

2014 Graduate Bootcamp: TDD

Not about testing!

Not talking about a release cycle here (days/weeks/months).
Fast cycle while we’re writing the code (seconds/minutes).

Important step!
Refactoring means improving the designwithout changing behaviour (eg removing duplicated code).
TDD allows you to design incrementally, while only implementing what’s needed.
Separate talk later.

“Traditional” process.
Looks linear, but problems will be found in design and implementation.

Reduce the amount of time it takes to test by automating.
From now on, whenever anyone says “test”, you can generally assume they mean “automated test”.

Writing the tests first forces us to think about what each piece of code has to do.
Still needs the design to have been decided up-front though.

Now we have that TDD loop.
Run tests first to make sure they fail correctly, with useful messages.

Definition of BDD has changed over time.

From Uncle Bob Martin
(that’s not one of the rules)

“Black box”. Does the whole system work as required?
Generally means testing through the user interface.
Tend to be slow, so you don’t want many of them.

Test classes along with their collaborators.
May hit things like databases.
Faster than system tests, but can still be slow.
Can be brittle – changes to a collaborator can break unrelated test.
Give confidence that things are wired together right.

Genuine unit tests – test a single class, with test doubles standing in for collaborators.
Fast and isolated.
We’ll cover test doubles tomorrow.

Generally acceptance tests are more likely to be system or integrated tests, but they don’t have to be (eg validation rules).

Fewer slow system tests at the top. Mostly fast unit tests.

Arrange/act/assert
Given/when/then
Ignore the syntax.

Name tests well.

No need for repetitive manual testing – can concentrate on exploratory testing.
You get used to assuming high test coverage – gives confidence.

Yes – at the small scale fo individual changes.
Much less debugging.
Vastly reduced design/test phases.

Kerry Buckley

October 27, 2014
Tweet

More Decks by Kerry Buckley

Other Decks in Programming

Transcript

  1. Test-driven development (according to Wikipedia) Test-driven development (TDD) is a

    software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
  2. Test-driven development (according to Wikipedia) Test-driven development (TDD) is a

    software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
  3. Test-driven development (according to Wikipedia) Test-driven development (TDD) is a

    software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
  4. Test-driven development (according to Wikipedia) Test-driven development (TDD) is a

    software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
  5. Test-driven development (according to Wikipedia) Test-driven development (TDD) is a

    software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
  6. Test-driven development (according to Wikipedia) Test-driven development (TDD) is a

    software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
  7. Outer and inner loops ATDD or BDD (maybe) Red Refactor

    Green Executable examples Release Customer
  8. Rules of TDD 1. You are not allowed to write

    any production code unless it is to make a failing unit test pass. 2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. 3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
  9. Rules of TDD 1. You are not allowed to write

    any production code unless it is to make a failing unit test pass. 2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. 3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
  10. Rules of TDD 1. You are not allowed to write

    any production code unless it is to make a failing unit test pass. 2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. 3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
  11. Acceptance tests Does the system behave as it should? Unit

    tests Do individual pieces work correctly?
  12. Anatomy of a test # Given some test accounts account_1

    = Account.new(100) account_2 = Account.new(50) ➊ Arrange
  13. Anatomy of a test # Given some test accounts account_1

    = Account.new(100) account_2 = Account.new(50) ➊ Arrange ➋ Act
  14. Anatomy of a test # Given some test accounts account_1

    = Account.new(100) account_2 = Account.new(50) # When I transfer money transfer(20, from: account_1,
 to: account_2) ➊ Arrange ➋ Act
  15. Anatomy of a test # Given some test accounts account_1

    = Account.new(100) account_2 = Account.new(50) # When I transfer money transfer(20, from: account_1,
 to: account_2) ➊ Arrange ➋ Act ➌ Assert
  16. Anatomy of a test # Given some test accounts account_1

    = Account.new(100) account_2 = Account.new(50) # When I transfer money transfer(20, from: account_1,
 to: account_2) # Then the balances should be updated expect(account_1.balance).to eq 80 expect(account_2.balance).to eq 70 ➊ Arrange ➋ Act ➌ Assert
  17. A good test… Expresses the programmer’s intent Gives confidence that

    the code works Only tests one thing Gives clear failure messages
  18. A good test… Expresses the programmer’s intent Gives confidence that

    the code works Only tests one thing Gives clear failure messages Is independent of other tests
  19. It’s not a unit test if… It talks to the

    database It communicates across the network
  20. It’s not a unit test if… It talks to the

    database It communicates across the network It touches the file system or reads config info
  21. It’s not a unit test if… It talks to the

    database It communicates across the network It touches the file system or reads config info It uses the current time or random numbers
  22. It’s not a unit test if… It talks to the

    database It communicates across the network It touches the file system or reads config info It uses the current time or random numbers It depends on non-deterministic behaviour
  23. It’s not a unit test if… It talks to the

    database It communicates across the network It touches the file system or reads config info It uses the current time or random numbers It depends on non-deterministic behaviour It can't run at the same time as other unit tests
  24. It’s not a unit test if… It talks to the

    database It communicates across the network It touches the file system or reads config info It uses the current time or random numbers It depends on non-deterministic behaviour It can't run at the same time as other unit tests You have to modify your environment to run it
  25. A good test suite… Gives confidence that the system works

    Runs quickly Is well-maintained Isolates each area under test
  26. Benefits of TDD Less manual testing required Faster feedback Make

    it safe to change code Reduced rework and debugging
  27. Benefits of TDD Less manual testing required Faster feedback Make

    it safe to change code Reduced rework and debugging Improved design
  28. How do I start? Greenfield project? JFDI! Otherwise… Automate highest

    value tests first Important features Where the most bugs occur
  29. How do I start? Greenfield project? JFDI! Otherwise… Automate highest

    value tests first Important features Where the most bugs occur Use TDD for new features
  30. How do I start? Greenfield project? JFDI! Otherwise… Automate highest

    value tests first Important features Where the most bugs occur Use TDD for new features Add tests for bugs when they’re found
  31. How do I start? Greenfield project? JFDI! Otherwise… Automate highest

    value tests first Important features Where the most bugs occur Use TDD for new features Add tests for bugs when they’re found Practice!