Stack kata

Objective

  • Create production code from test

  • Start from assertions

Connection - 10'

What are the key principles to write good unit tests ?

Concepts - 10'

Tip for deciding the first test to write: The simplest possible.

  • Red, Green, Refactor

    • Add a test

    • Run all tests and see if the new one fails - Red

    • Write the minimum amount of code to pass the failing test -

    • Run tests - Green

    • Refactor code - Blue

    • Repeat

  • TDD Golden Rule

    • Do not write any production code until you have a failing test that requires it

  • Arrange, Act, Assert

    • Arrange :

      • Setup everything needed for the testing code

      • Data initialization / mocks

    • Act :

      • Invoke the code under test / behavior

    • Assert :

      • Specify the pass criteria for the test

Define a test checklist for your team :

Concrete Practice - 25'

  • In pair, implement a Stack class with the following public methods by using TDD :

  • Stack should throw an exception if popped when empty

  • Start by writing your assertions

When you use TDD :

  • Split your screen

    • Your tests at the left

    • Your production code at the right

Questions to ask :

  • What is the value to test the push ?

    • Do not change your production code encapsulation for testing purpose

  • Often developers want to inherit from ArrayList or another types of collection

    • Ask them Why

    • Remind them that the purpose of TDD is also to avoid writing unnecessary code

      • In the exercise, is it required to have a Count function on the Stack for example ?

After 20' of coding ask them to demonstrate what has been written

Here is a solution : https://github.com/ythirion/stack-kata

Ask to the participants what do they think about the naming of the tests.

Conclusion - 5'

Note the main thing you want to remember about this session on a Sticky Note

Resources

Last updated

Was this helpful?