I made an unbeatable tic-tac-toe game a while back and I thought it would make a good project to learn code testing. I have written some tests, but actually trying it has lead to some questions about implementation.
The part I’m particularly concerned with adding tests to is the logic that determines what move the computer should make based on the game state. I’m struggling to decide what would constitute a unit of work. The structure of this logic, in broad strokes, is someting like:
| calculateCompMove() | | subfunction 1 of calculateCompMove() | | | helperFunction1 | | | helperFunction2 | | | helperFunction3 | | subfunction 2 of calculateCompMove() | | | helperFunction1 | | | helperFunction2 | | | helperFunction3 | | subfunction 3 of calculateCompMove() | | | helperFunction1 | | | helperFunction2 | | | helperFunction3
The helper functions are pretty small, 1-4 lines usually.
- Would it be trivial to test these?
- Should I just unit test subfunctions and calculateCompMove? If so, would you describe the testing for calculateCompMove as integration testing because it is testing the integration between all the subfunctions?
- If the helpers are only 1-4 lines and not repeated elsewhere in my codebase should they even be extracted out as separate functions?
You can see an example of one of the subfunctions and it’s helper functions here.