When developing in your spare time automated tests are your best friend


Well I just finished fixing a bug in chrss and it's made me very glad that I've been using automated tests.

The bug looked like it might be very tricky to track down and I feared that it might get messy. In the end it proved to be a slight edge case I'd not catered for and was pretty easy to fix.

First I quickly tracked down the stack-trace in my logs:

  File "/home2/lilspikey/webapps/chrss2/chrss/controllers/game.py", line 226, in move
    game.make_move(game.turn,move)
  File "/home2/lilspikey/webapps/chrss2/chrss/model.py", line 239, in make_move
    self._record_move(chess_game,color,move)
  File "/home2/lilspikey/webapps/chrss2/chrss/model.py", line 202, in _record_move
    incheck=chess_game.in_mate()
  File "/home2/lilspikey/webapps/chrss2/chrss/chess2.py", line 213, in in_mate
    return self._board.calc_mate(self.game_state)
  File "/home2/lilspikey/webapps/chrss2/chrss/chess2.py", line 915, in calc_mate
    can_move=self.any_legal_moves(game_state)
  File "/home2/lilspikey/webapps/chrss2/chrss/chess2.py", line 990, in any_legal_moves
    legal=self.legal_moves(game_state,pos)
  File "/home2/lilspikey/webapps/chrss2/chrss/chess2.py", line 984, in legal_moves
    return self._filter_check_moves(game_state,moves)
  File "/home2/lilspikey/webapps/chrss2/chrss/chess2.py", line 1029, in _filter_check_moves
    board.move_piece(move) # make the move
  File "/home2/lilspikey/webapps/chrss2/chrss/chess2.py", line 1103, in move_piece
    moved_piece,taken_piece,castled,enpassant,updated_positions=self._calc_move(move)
  File "/home2/lilspikey/webapps/chrss2/chrss/chess2.py", line 1081, in _calc_move
    raise ValueError("illegal castling move")
ValueError: illegal castling move

Next step was to create a unit test to replay the moves from the game with the problem. Once I'd got it failing in the same way, I discovered a bug in my logic that meant rooks captured without moving would not update the "castling status" for that side. This led to falsely generating castling moves that weren't possible and thus the failure in the stack trace (some defensive coding to stop this kind of thing). In this game in particular white's queen-side rook had been captured without moving and my chess module was reporting that the king could still castle queen-side!

After getting it to work I now had a nice automated regression test for that bug. I also took the time to add a unit test for the actual function that I changed.

Brilliant stuff. Now I can be extra certain that bug is fixed and will stay fixed, as those tests will get run every time I run my test suite. I'm working on chrss in my spare time, so this is really important to me. I really don't have time to spend manually testing and verifying that _everything_ works.

The more testing I can automate the less testing I have to do. Which means I've got more time for adding features or else playing games of chess!