Recognizable? You’re happily programming but after a while you realize you broke something. Which of the many changes caused the problem?
Or, you finally made your program work and now you don’t dare to make any changes (to tidy things up) because that means checking all over again if everything still works. Especially if you have to provide input or read prints this is very time consuming. For such cases I wrote a follow-up to this article.
These problems start to arise pretty soon in your programming journey, typically when your programs approach 30 or so lines of code. Problems indeed: you’re spending time and energy on sleuthing or manually testing, not on improving!
The solution is writing some additional code that does the testing of your ‘actual’ code. Run these tests (by running your code as you would normally) each time you make a small change. This way you immediately know if either everything still works or exactly what caused a problem (that last small change).
If you look up how to test you might be daunted by all the libraries, frameworks and methodologies that you find. However, for a beginner they are probably overkill.
Instead, let’s look at the simplest way to get benefits from writing automated tests. You can test in this way in any langauge, no libraries or frameworks required.
The next section gives you some vocubulary to make it easier to talk about what we’re doing. In the section after that we start writing code.
Vocabulary
A test is a check to verify if something works as expected.
You have tested your code many times yourself. You wrote a program, ran it and checked if what it printed is what it should have printed.
If you or another human performs the test it’s called a manual test.
If other code performs the test it’s called an automated test.
Code that does such testing is test code.
Code that is tested by test code is production code. You might consider this the ‘actual’ code, the code that makes the program do what you want it to do.
In testing we look at what code does, not at the code itself. You test the behavior of code, not its structure. Structure is what code is used and how it’s organized.
Behavior is what can be observed from the outside. In a manual test the behavior you typically look at is what the program prints on screen. Other behavior can be things like how fast it runs.
There is also behavior to be observed inside your program. For example the behavior of a function is what value it will return, given a certain input. This is the basis of the simple testing method which we’ll get to shortly.
Behavior dictates the correctness of code and is very objective. Does the code do what it should do, yes or no? Assuming you actually know what the code should do, but that’s out of scope for this article. While learning programming the exercises state clearly what the code should do.
Structure dictates things like how easy it is to understand the code and how easy it is to change or expand the code. The larger your program becomes the more important it is to have good structure. Compared to behavior the quality of structure is much more subjective.
Especially as a beginner your first goal is to write code that ‘works’. Focus on getting the correct behavior without focussing too much on structure. Don’t neglect structure entirely though, because a little structure will help you get to ‘it works!’ faster.
After you’ve achieved the behavior you want you can change (improve) the structure of your code. Changing structure while keeping behavior the same is called refactoring.
Writing tests
The simple tools we’re going to use for our automated tests are assertions and functions. Both are available in all programming languages.
We’re going to look at the following workflow:
- Write code with correct behavior
- Write test code that ‘pins down’ this behavior
- Wrap the production code in a function
- for each test case
- call that function with case-specific arguments
- assert that the returned value is equal to the expected value
- Refactor the production code
- After each small change, run the test code
- Do this easily with a keybinding!
- Test failed? Undo change or investigate
Assertions
Details differ slightly between languages but in general assertions work like this. When running assert something
, if something
evaluates to the value true
of type boolean then nothing happens, otherwise we get a nice big fat error.
To keep this article easy to read I deliberately chose a code sample that is short, not one that best illustrates when you want to start using automated tests. Please sprinkle some imagination on top of it. You don’t need to understand the code sample or the puzzle the code is a solution to.
A client of mine came to me with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
direction = ">^^v^<>v<<<v<v^>>v^^^<v<>^^><^<<^vv>>>^<<^>><vv<<v^<^^><>>><>v<><>^^<^^^<><>>vv>vv>v<<^>v<>^>v<v^<>v>><>^v<<<<v^vv^><v>v^>>>vv>v^^^<^^<>>v<^^v<>^<vv^^<^><<>^>><^<>>><><vv><>v<<<><><>v><<>^^^^v>>^>^<v<<vv^^<v<^<^>^^v^^^^^v<><^v><<><^v^>v<<>^<>^^v^<>v<v^>v>^^<vv^v><^<>^v<><^><v^><><><<<<>^vv^>^vvvvv><><^<vv^v^v>v<<^<^^v^<>^<vv><v<v^v<<v<<^^>>^^^v^>v<><^vv<<^<>v<v><><v^^><v<>^^>^^>v^>^<<<<v><v<<>v><^v>^>><v^^<^>v<vvvv<>>>>>^v^^>v<v<^<vv>^>^vv^>vv^^v<<^<^^<>v>vv^v>><>>>v^>^>^^v<>^<v<<>^vv>v^<<v>v<<><v>^vvv<v<vvv^v<vv<v^^^>v><<^<>><v^^>^v^>>^v<^<><v<>>v^<>>v<>>v^^^><^>>vvvv>^v<^><<>>^<>^>vv><v<<>>^^>v^^^><^<<^^v>v<^<<>v>^^vvv^v^>v^<>^^<>v^v>v>v<v^>vv>^^v<>v>>^<>><>v>v^<<vvvv<vvv><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<<>vv<^v>><<>vvv<><<vv<<<^>v^^>>^>^v>><><^^v<>><>>v^>^<vv><<<>><><<v>^^<>>v<><^<vv>vv<^v>^<<<<v<^<<^^>>^<><^>><<>^>v>^^^v>>^<^^v><v^v>^><<><>>^>>^<<v<>^v<>^>^<v>>vv>^vvv<<v<<^>^>^<<^^<>^^^^vvv<>^vv<vvvvv^^>^^<^>>><>v^<><^<<^>v^^v<>>^vv<>v^^<>>v^vvvvv<<v^<v^^>>><vvvvv>><^>vv>v^v^<v<^>^^><^>^^^^v<><^v<<>v^>v>>vv<<>^<v^^>vvv>^^<v^<>vv^><>><v^^v<>^>>^>v><>>^^v>^>^>>>^>v<^v>v>^<^^^^^>>v<v<>>v<<^>^<v<<>^^>><<^><>v<>^^^vv<>^^>><<^^>v>vv>vv>v^>^v>v^^<>>><<v><v<<>>v><>vvv^^v>^^>^vvvv^>^<>^vvvv><v><v<>>><>^<^vv<>^v<^v<>^vvv<<>><vvv^>>^><<vv^<v^>^<v<<^^>^^<^^v^>v<>v^v><>><v^^>>^vvv><^vv>v^<^<^v>>v^^>^vvv^<v^^v^^>v<^<>>^<>>>^^<><^^vv<>^vv^<>>>>^^<<^^<>vv^^><>^^<v<<v>^<v^^>^v<><><>vvv>^v^>>vv<<^v<<>><v>^><^>>>^<^<^^>vv^<<^<>>^^><><<v>^^<v>>v<<vvvv>^v^vv>><^^<<^>>v>v<^^^<^><^^vv>^vv<^<vv<>v><^<><v><^^^>>^<><^<v>>>>v^<v>>>>>v<><^^>v<^<^>><v<>^>vv>^^v^v^<<v<><<<^v^><<^<><<<<v<^>><<<>v>>vv><vv<><<^<^<><vv>^^^^<>v<<<<v>vv<>vv^^^>><>vv^><>>^vv<<><^^vv<>v^>>^<<>^<v^<^>v<"
loc = [0, 0]
totalList = [str(loc)]
for i in range(0, len(direction)):
if direction[i] == '>':
loc[0] += 1
elif direction[i] == '<':
loc[0] -= 1
elif direction[i] == '^':
loc[1] += 1
else:
loc[1] -= 1
totalList.append(str(loc))
totalSet = set(totalList)
print(len(totalSet))
|
The input from the puzzle description is the very long string >^^v^...
. The program prints 2592
. If I submit this to the puzzle website it tells me it’s the right answer. The program has correct behavior.
I could run the program after each small change I make. If 2592
is still printed, the change was good. But I have to read pretty carefully and confirm all 4 digits. If this were a program that should print a list of 10 things, manually checking would start to get really tedious.
Let’s try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
direction = ">^^v^<>v<<<v<v^>>v^^^<v<>^^><^<<^vv>>>^<<^>><vv<<v^<^^><>>><>v<><>^^<^^^<><>>vv>vv>v<<^>v<>^>v<v^<>v>><>^v<<<<v^vv^><v>v^>>>vv>v^^^<^^<>>v<^^v<>^<vv^^<^><<>^>><^<>>><><vv><>v<<<><><>v><<>^^^^v>>^>^<v<<vv^^<v<^<^>^^v^^^^^v<><^v><<><^v^>v<<>^<>^^v^<>v<v^>v>^^<vv^v><^<>^v<><^><v^><><><<<<>^vv^>^vvvvv><><^<vv^v^v>v<<^<^^v^<>^<vv><v<v^v<<v<<^^>>^^^v^>v<><^vv<<^<>v<v><><v^^><v<>^^>^^>v^>^<<<<v><v<<>v><^v>^>><v^^<^>v<vvvv<>>>>>^v^^>v<v<^<vv>^>^vv^>vv^^v<<^<^^<>v>vv^v>><>>>v^>^>^^v<>^<v<<>^vv>v^<<v>v<<><v>^vvv<v<vvv^v<vv<v^^^>v><<^<>><v^^>^v^>>^v<^<><v<>>v^<>>v<>>v^^^><^>>vvvv>^v<^><<>>^<>^>vv><v<<>>^^>v^^^><^<<^^v>v<^<<>v>^^vvv^v^>v^<>^^<>v^v>v>v<v^>vv>^^v<>v>>^<>><>v>v^<<vvvv<vvv><v^<^>^v<>>^><v>><>^<v>v<v>vv^>>vvv<>v>v<v^>>^>>v<<>^<>^<>>>^v<<<^<^v>vv^>><<><v^>^v^^^v<>^^vv><>><>>^>v^<v<>v<>>^<<^v>^^^<>^v^><>v<<v>vv^>vv<<>>><<^v^<>v<vv>>>^^<>^><<^>vv>>^<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<^^<>^^^^vvv<>^vv<vvvvv^^>^^<^>>><>v^<><^<<^>v^^v<>>^vv<>v^^<>>v^vvvvv<<v^<v^^>>><vvvvv>><^>vv>v^v^<v<^>^^><^>^^^^v<><^v<<>v^>v>>vv<<>^<v^^>vvv>^^<v^<>vv^><>><v^^v<>^>>^>v><>>^^v>^>^>>>^>v<^v>v>^<^^^^^>>v<v<>>v<<^>^<v<<>^^>><<^><>v<>^^^vv<>^^>><<^^>v>vv>vv>v^>^v>v^^<>>><<v><v<<>>v><>vvv^^v>^^>^vvvv^>^<>^vvvv><v><v<>>><>^<^vv<>^v<^v<>^vvv<<>><vvv^>>^><<vv^<v^>^<v<<^^>^^<^^v^>v<>v^v><>><v^^>>^vvv><^vv>v^<^<^v>>v^^>^vvv^<v^^v^^>v<^<>>^<>>>^^<><^^vv<>^vv^<>>>>^^<<^^<>vv^^><>^^<v<<v>^<v^^>^v<><><>vvv>^v^>>vv<<^v<<>><v>^><^>>>^<^<^^>vv^<<^<>>^^><><<v>^^<v>>v<<vvvv>^v^vv>><^^<<^>>v>v<^^^<^><^^vv>^vv<^<vv<>v><^<><v><^^^>>^<><^<v>>>>v^<v>>>>>v<><^^>v<^<^>><v<>^>vv>^^v^v^<<v<><<<^v^><<^<><<<<v<^>><<<>v>>vv><vv<><<^<^<><vv>^^^^<>v<<<<v>vv<>vv^^^>><>vv^><>>^vv<<><^^vv<>v^>>^<<>^<v^<^>v<"
loc = [0, 0]
totalList = [str(loc)]
for i in range(0, len(direction)):
if direction[i] == '>':
loc[0] += 1
elif direction[i] == '<':
loc[0] -= 1
elif direction[i] == '^':
loc[1] += 1
else:
loc[1] -= 1
totalList.append(str(loc))
totalSet = set(totalList)
assert len(totalSet) == 2592
|
If I now make a change, if everything is still OK my console remains empty. If I broke something, I get something like
1
2
3
4
|
Traceback (most recent call last):
File "/home/xyz/projects/example.py", line 16, in <module>
assert len(totalSet) == 2592
AssertionError
|
Very different from an empty console! By using the assertion it becomes much easier to see if a change broke something. When you do get an assertion error you can revert your change or investigate further.
The test is in the same file as your production code, so to run the test just run the code as you would normally.
Your IDE or whatever you use to write code in probably has a way to run your code with the press of a single key, or a combination of two keys. If you make it a habit to press that run key whenever you pause to think, you run your tests very frequently and quickly notice it when you introduce a mistake.
Multiple cases
Very often you want your program to work with different inputs. This is the case for our puzzle as well. For our puzzle, you can only get an input if you log in, and each user gets a unique input.
We want to test if our code works (and stays working after changes) for multiple different inputs.
A very time consuming way to do this is to change some values to match the test case and run the file, anew for each test case…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
direction = "^>v<"
loc = [0, 0]
totalList = [str(loc)]
for i in range(0, len(direction)):
if direction[i] == '>':
loc[0] += 1
elif direction[i] == '<':
loc[0] -= 1
elif direction[i] == '^':
loc[1] += 1
else:
loc[1] -= 1
totalList.append(str(loc))
totalSet = set(totalList)
assert len(totalSet) == 4
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
direction = "^v^v^v^v^v^v^v"
loc = [0, 0]
totalList = [str(loc)]
for i in range(0, len(direction)):
if direction[i] == '>':
loc[0] += 1
elif direction[i] == '<':
loc[0] -= 1
elif direction[i] == '^':
loc[1] += 1
else:
loc[1] -= 1
totalList.append(str(loc))
totalSet = set(totalList)
assert len(totalSet) == 2
|
A better way is to introduce a function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def puzzle_answer(direction):
loc = [0, 0]
totalList = [str(loc)]
for i in range(0, len(direction)):
if direction[i] == '>':
loc[0] += 1
elif direction[i] == '<':
loc[0] -= 1
elif direction[i] == '^':
loc[1] += 1
else:
loc[1] -= 1
totalList.append(str(loc))
totalSet = set(totalList)
return len(totalSet)
assert puzzle_answer("^v^v^v^v^v^v^v") == 2
|
We wrapped the production code in a function. What we want to change each test case is now a parameter of the function. The value that we want to assert is returned by the function. A test case is calling the function with a specific argument and asserting the return value.
We can now easily have multiple test cases:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def puzzle_answer(direction):
loc = [0, 0]
totalList = [str(loc)]
for i in range(0, len(direction)):
if direction[i] == '>':
loc[0] += 1
elif direction[i] == '<':
loc[0] -= 1
elif direction[i] == '^':
loc[1] += 1
else:
loc[1] -= 1
totalList.append(str(loc))
totalSet = set(totalList)
return len(totalSet)
assert puzzle_answer("^v^v^v^v^v^v^v") == 2
assert puzzle_answer("^>v<") == 4
assert puzzle_answer(">^^v^<>v<<<v<v^>>v^^^<v<>^^><^<<^vv>>>^<<^>><vv<<v^<^^><>>><>v<><>^^<^^^<><>>vv>vv>v<<^>v<>^>v<v^<>v>><>^v<<<<v^vv^><v>v^>>>vv>v^^^<^^<>>v<^^v<>^<vv^^<^><<>^>><^<>>><><vv><>v<<<><><>v><<>^^^^v>>^>^<v<<vv^^<v<^<^>^^v^^^^^v<><^v><<><^v^>v<<>^<>^^v^<>v<v^>v>^^<vv^v><^<>^v<><^><v^><><><<<<>^vv^>^vvvvv><><^<vv^v^v>v<<^<^^v^<>^<vv><v<v^v<<v<<^^>>^^^v^>v<><^vv<<^<>v<v><><v^^><v<>^^>^^>v^>^<<<<v><v<<>v><^v>^>><v^^<^>v<vvvv<>>>>>^v^^>v<v<^<vv>^>^vv^>vv^^v<<^<^^<>v>vv^v>><>>>v^>^>^^v<>^<v<<>^vv>v^<<v>v<<><v>^vvv<v<vvv^v<vv<v^^^>v><<^<>><v^^>^v^>>^v<^<><v<>>v^<>>v<>>v^^^><^>>vvvv>^v<^><<>>^<>^>vv><v<<>>^^>v^^^><^<<^^v>v<^<<>v>^^vvv^v^>v^<>^^<>v^v>v>v<v^>vv>^^v<>v>>^<>><>v>v^<<vvvv<vvv><v^<^>^v<>>^><v>><>^<v>v<v>vv^>>vvv<>v>v<v^>>^>>v<<>^<>^<>>>^v<<<^<^v>vv^>><<><v^>^v^^^v<>^^vv><>><>>^>v^<v<>v<>>^<<^v>^^^<>^v^><>v<<v>vv^>vv<<>>><<^v^<>v<vv>>>^^<>^><<^>vv>>^<<v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^<>^^^^vvv<>^vv<vvvvv^^>^^<^>>><>v^<><^<<^>v^^v<>>^vv<>v^^<>>v^vvvvv<<v^<v^^>>><vvvvv>><^>vv>v^v^<v<^>^^><^>^^^^v<><^v<<>v^>v>>vv<<>^<v^^>vvv>^^<v^<>vv^><>><v^^v<>^>>^>v><>>^^v>^>^>>>^>v<^v>v>^<^^^^^>>v<v<>>v<<^>^<v<<>^^>><<^><>v<>^^^vv<>^^>><<^^>v>vv>vv>v^>^v>v^^<>>><<v><v<<>>v><>vvv^^v>^^>^vvvv^>^<>^vvvv><v><v<>>><>^<^vv<>^v<^v<>^vvv<<>><vvv^>>^><<vv^<v^>^<v<<^^>^^<^^v^>v<>v^v><>><v^^>>^vvv><^vv>v^<^<^v>>v^^>^vvv^<v^^v^^>v<^<>>^<>>>^^<><^^vv<>^vv^<>>>>^^<<^^<>vv^^><>^^<v<<v>^<v^^>^v<><><>vvv>^v^>>vv<<^v<<>><v>^><^>>>^<^<^^>vv^<<^<>>^^><><<v>^^<v>>v<<vvvv>^v^vv>><^^<<^>>v>v<^^^<^><^^vv>^vv<^<vv<>v><^<><v><^^^>>^<><^<v>>>>v^<v>>>>>v<><^^>v<^<^>><v<>^>vv>^^v^v^<<v<><<<^v^><<^<><<<<v<^>><<<>v>>vv><vv<><<^<^<><vv>^^^^<>v<<<<v>vv<>vv^^^>><>vv^><>>^vv<<><^^vv<>v^>>^<<>^<v^<^>v<"
) == 2592
|
There you have it, the simplest way to add automated tests. As you start writing more complicated programs, eventually this way of testing will no longer be powerful enough. That being said, even senior professionals use this form of testing in small informal projects.
Probably the first case you’ll encounter as a beginner in which this form of testing no longer works nicely is when the desired behavior of your program involves processing user input and printing to the console, such as for small games. In a follow-up article I build upon the way of testing described here to tackle such more complicated programs.
Closing words
Testing is a very large topic within programming. Hopefully I succeeded in protecting you, dear presumed beginner, from drowning in advanced tools and concepts while still providing you with useful new information.
To tease you with some of interesting things the world of testing has to offer:
Some programmers swear by first starting with a failing test and only then writing production code that makes the test pass. This is called ‘test driven development’.
Some programmers see tests as documentation. When they try to understand a codebase they first study the tests to observe what its behavior is, before diving into its structure.
Some programmers use testing as a means to gain confidence in their product. However, legendary Edsger Dijkstra famously said “Program testing can be used to show the presence of bugs, but never to show their absence!”. So how to test in such a way to still be confident?
Some programmers prefer to test only at the very edges of their system, some prefer to also test at checkpoints within their system to obtain more detailed information. This is a game of preventing so called brittle tests (tests that depend more on structure than they should) with smart architecture.
Some programmers… yes, on most things programmers have not reached a consensus. Perhaps the only two things that come close are that naming is hard and go-to statements are considered harmful.