TDD appointment for beginners?

I want to introduce the discipline driven by Test Driven Development to my junior Java programmers who have never heard this term before. I plan to have a session explaining the basics and benefits of TDD in the first half, and then go through a practical assignment in the second half.

So, what could be a good Java based assignment for TDD newbies? It should not be so trivial that people lose interest, and not so difficult that they lose heart. I have seen several online, but also want to consider your suggestions. Any pointers / links to them are greatly appreciated.

+7
source share
7 answers

Here is a non-trivial TDD tutorial in which the topic is written by Tetris. It will probably take more than 10 hours to complete it. About 30 of the first tests were pre-written, and after that some tips were given as to which tests should be written next.

https://github.com/orfjackal/tdd-tetris-tutorial

+4
source

An example that I like to use when demonstrating TDD is password validation: write code that checks if the string is a strong enough password. I like it because:

  • everyone understands the problem, and it does not depend on the language
  • you can start with one or two rules that are easy to check (it is not "PASSWORD", has more than 8 characters ...) to get the TDD beat freezing.
  • you quickly get into interesting questions when you have 2 or more rules. Testing becomes difficult and leads to redesign / re-factoring, separating the rules from the validator, and getting code that is clean and easy to test.
+3
source

I like Range for this - just a whole series of integers. Record includes (int), overlaps (Range) and any similar methods; you can find out if it is completely open, completely closed or half open (the best answer, basically), and let the tests take you there.

Natural Sort is also helpful. Practical, rewarding and driving experience can lead to unexpected decisions.

+2
source

I like to write the engines of John Conway’s Game of Life .

Very simple rules. They can be written in one (slightly dirty) class, but the real joy is when you start thinking about the responsibility of classes, and then exhausting these responsibilities - for example, a neighborhood calculator, rules, etc.

You can also choose the best design with a few additional rules:

  • The grid can switch back and forth at run time from a restricted grid (without which nothing can survive) to an infinite grid or a wrapped grid (gliders return from the other side).
  • Cells change color (or string representation) depending on how old they are (moves the project from a value object to an object).
  • Rules can be changed from Conway to High Life.

If you work graphically, you can have even more fun. Developers can use tests or scripts to talk around the edges (the interaction between different stories becomes complicated):

  • Cells change size depending on the number of neighbors
  • There is a back button
  • There is a joystick that allows you to move around wrapped or endless grids.
  • The fee can be (re) installed on predefined scripts
  • The fee may change size.

This is what Corey Haines always uses in his indentation code. I used it to train TDD / unit-level BDD for many years. You can quickly see some progress, then it gets harder when it grows.

+1
source

I used incremental search in more than one class with great results. People know this scenario from their mobile phones; the address book usually works this way (you enter the name of the name, and it displays all the names starting with these letters).

One of the advantages is that this problem can be really incremental. You will learn the details when you are already doing this. And you can add additional features, for example:

  • last application (the most common names appear): this requires the incremental search object to maintain state
  • alphabetical order
  • looking at letters in any position (for example, a search for "JSm" finds "John Smith")

However, if you do this in class, I recommend that you have about 3 hours. This is not a quick example. And he talks a lot about lists, so make sure your students are familiar with them, including Array.asList, is preferable.

+1
source

Tic-tac-toe game. Ask the player to go first, the computer goes second.

You can start with a computer, which will always be in the middle, but then you need to pass a test to make sure that the computer does not select a place that has already been taken. Then you can slowly start testing to make sure that the computer always covers its vulnerabilities.

0
source

(not an answer, just commenting, please ignore.)

Testing should not be a programming center. Especially for young students. Whenever we can, we must first focus on the logic and argumentation of the implementation. We need to practice our skills with such confidence that we know that the code is correct, simply because we wrote it.

Is this an incredible goal? Many people can get very close to it. But even if it is too unattainable for a person, he should practice this way, at least when he is a student. It will not hurt.

TDD seems to encourage students to do a good enough job to pass the tests. You do not need to think carefully about your algorithm; just adjust it until it passes the tests, which is pretty simple.

All bad things will come out of him.

I am not saying that testing is not important. But for the sake of correctness it should not be 1st .

In another related topic, some people claim that TDD is good for design: if the code is easy to test, it is most likely a good design. It is also very puzzling - when you can not focus on good design?

0
source

All Articles