Skip to content

Commit 81b7a79

Browse files
Merge pull request #10 from ResearchCodingClub/simplify_early_material
Simplify early material
2 parents 9b358f2 + ef6bb72 commit 81b7a79

File tree

2 files changed

+110
-124
lines changed

2 files changed

+110
-124
lines changed

episodes/01-why-test-my-code.Rmd

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ exercises: 2
1818

1919
## What is software testing?
2020

21-
Software testing is the process of checking that code is working as expected. You may have data processing functions or automations that you use in your work - how do you know that they are doing what you expect them to do?
21+
Software testing is the process of checking that code is working as expected.
22+
You may have data processing functions or automations that you use in your work.
23+
How do you know that they are doing what you expect them to do?
2224

23-
Software testing is most commonly done by writing code (tests) that check that your code works as expected.
25+
Software testing is most commonly done by writing test code that check that
26+
your code works as expected.
2427

25-
This might seem like a lot of effort, so let's go over some of the reasons you might want to add tests to your project.
28+
This might seem like a lot of effort, so let's go over some of the reasons you
29+
might want to add tests to your project.
2630

2731

2832
## Catching bugs
2933

30-
Whether you are writing the occasional script or developing a large software, mistakes are inevitable. Sometimes you don't even know when a mistake creeps into the code, and it gets published.
34+
Whether you are writing the occasional script or developing a large software,
35+
mistakes are inevitable. Sometimes you don't even know when a mistake creeps
36+
into the code, and it gets published.
3137

3238
Consider the following function:
3339

@@ -36,22 +42,32 @@ def add(a, b):
3642
return a - b
3743
```
3844

39-
When writing this function, I made a mistake. I accidentally wrote `a - b` instead of `a + b`. This is a simple mistake, but it could have serious consequences in a project.
45+
When writing this function, I made a mistake. I accidentally wrote `a - b`
46+
instead of `a + b`. This is a simple mistake, but it could have serious
47+
consequences in a project.
4048

41-
When writing the code, I could have tested this function by manually trying it with different inputs and checking the output, but:
49+
When writing the code, I could have tested this function by manually trying it
50+
with different inputs and checking the output, but:
4251

4352
- This takes time.
4453
- I might forget to test it again when we make changes to the code later on.
45-
- Nobody else in my team knows if I tested it, or how I tested it, and therefore whether they can trust it.
54+
- Nobody else in my team knows if I tested it, or how I tested it, and
55+
therefore whether they can trust it.
4656

4757
This is where automated testing comes in.
4858

4959
## Automated testing
5060

51-
Automated testing is where we write code that checks that our code works as expected. Every time we make a change, we can run our tests to automatically make sure that our code still works as expected.
61+
Automated testing is where we write code that checks that our code works as
62+
expected. Every time we make a change, we can run our tests to automatically
63+
make sure that our code still works as expected.
5264

53-
If we were writing a test from scratch for the `add` function, think for a moment on how we would do it.
54-
We would need to write a function that runs the `add` function on a set of inputs, checking each case to ensure it does what we expect. Let's write a test for the `add` function and call it `test_add`:
65+
If we were writing a test from scratch for the `add` function, think for a
66+
moment on how we would do it.
67+
68+
We would need to write a function that runs the `add` function on a set of
69+
inputs, checking each case to ensure it does what we expect. Let's write a test
70+
for the `add` function and call it `test_add`:
5571

5672
```python
5773
def test_add():
@@ -66,16 +82,19 @@ def test_add():
6682
print("Test failed!")
6783
```
6884

69-
Here we check that the function works for a set of test cases. We ensure that it works for positive numbers, negative numbers, and zero.
85+
Here we check that the function works for a set of test cases. We ensure that
86+
it works for positive numbers, negative numbers, and zero.
7087

7188

7289
::::::::::::::::::::::::::::::::::::: challenge
7390

74-
## Challenge 1: What could go wrong?
91+
## What could go wrong?
7592

76-
When writing functions, sometimes we don't anticipate all the ways that they could go wrong.
93+
When writing functions, sometimes we don't anticipate all the ways that they
94+
could go wrong.
7795

78-
Take a moment to think about what is wrong, or might go wrong with these functions:
96+
Take a moment to think about what is wrong, or might go wrong with these
97+
functions:
7998

8099
```python
81100
def greet_user(name):
@@ -89,11 +108,12 @@ def gradient(x1, y1, x2, y2):
89108

90109
:::::::::::::::::::::::: solution
91110

92-
## Answer
93-
94-
The first function will incorrectly greet the user, as it is missing a space after "Hello". It would print `HelloAlice!` instead of `Hello Alice!`.
111+
The first function will incorrectly greet the user, as it is missing a space
112+
after "Hello". It would print `HelloAlice!` instead of `Hello Alice!`.
113+
114+
If we wrote a test for this function, we would have noticed that it was not
115+
working as expected:
95116

96-
If we wrote a test for this function, we would have noticed that it was not working as expected:
97117
```python
98118
def test_greet_user():
99119
if greet_user("Alice") != "Hello Alice!":
@@ -102,7 +122,8 @@ def test_greet_user():
102122

103123
The second function will crash if `x2 - x1` is zero.
104124

105-
If we wrote a test for this function, it may have helped us to catch this unexpected behaviour:
125+
If we wrote a test for this function, it may have helped us to catch this
126+
unexpected behaviour:
106127

107128
```python
108129
def test_gradient():
@@ -114,7 +135,7 @@ def test_gradient():
114135
print("Test failed!")
115136
```
116137

117-
And we could have ammened the function:
138+
And we could have amended the function:
118139

119140
```python
120141
def gradient(x1, y1, x2, y2):
@@ -129,7 +150,8 @@ def gradient(x1, y1, x2, y2):
129150

130151
## Finding the root cause of a bug
131152

132-
When a test fails, it can help us to find the root cause of a bug. For example, consider the following function:
153+
When a test fails, it can help us to find the root cause of a bug. For example,
154+
consider the following function:
133155

134156
```python
135157

@@ -143,45 +165,57 @@ def triangle_area(base, height):
143165
return divide(multiply(base, height), 2)
144166
```
145167

146-
There is a bug in this code too, but since we have several functions calling each other, it is not immediately obvious where the bug is. Also, the bug is not likely to cause a crash, so we won't get a helpful error message telling us what went wrong. If a user happened to notice that there was an error, then we would have to check `triangle_area` to see if the formula we used is right, then `multiply`, and `divide` to see if they were working as expected too!
168+
There is a bug in this code too, but since we have several functions calling
169+
each other, it is not immediately obvious where the bug is. Also, the bug is
170+
not likely to cause a crash, so we won't get a helpful error message telling us
171+
what went wrong. If a user happened to notice that there was an error, then we
172+
would have to check `triangle_area` to see if the formula we used is right,
173+
then `multiply`, and `divide` to see if they were working as expected too!
147174

148-
However, if we had written tests for these functions, then we would have seen that both the `triangle_area` and `multiply` functions were not working as expected, allowing us to quickly see that the bug was in the `multiply` function without having to check the other functions.
175+
However, if we had written tests for these functions, then we would have seen
176+
that both the `triangle_area` and `multiply` functions were not working as
177+
expected, allowing us to quickly see that the bug was in the `multiply`
178+
function without having to check the other functions.
149179

150180

151181
## Increased confidence in code
152182

153-
When you have tests for your code, you can be more confident that it works as expected. This is especially important when you are working in a team or producing software for users, as it allows everyone to trust the code. If you have a test that checks that a function works as expected, then you can be confident that the function will work as expected, even if you didn't write it yourself.
183+
When you have tests for your code, you can be more confident that it works as
184+
expected. This is especially important when you are working in a team or
185+
producing software for users, as it allows everyone to trust the code. If you
186+
have a test that checks that a function works as expected, then you can be
187+
confident that the function will work as expected, even if you didn't write it
188+
yourself.
154189

155190
## Forcing a more structured approach to coding
156191

157-
When you write tests for your code, you are forced to think more carefully about how your code behaves and how you will verify that it works as expected. This can help you to write more structured code, as you will need to think about how to test it as well as how it could fail.
192+
When you write tests for your code, you are forced to think more carefully
193+
about how your code behaves and how you will verify that it works as expected.
194+
This can help you to write more structured code, as you will need to think
195+
about how to test it as well as how it could fail.
158196

159197
::::::::::::::::::::::::::::::::::::: challenge
160198

161-
## Challenge 2: What could go wrong?
199+
## What could go wrong?
162200

163201
Consider a function that controls a driverless car.
164202

165203
- What checks might we add to make sure it is not dangerous to use?
166204

167205
```python
168-
169206
def drive_car(speed, direction):
170207

171208
... # complex car driving code
172209

173210
return speed, direction, brake_status
174-
175-
176211
```
177212

178213
:::::::::::::::::::::::: solution
179214

180-
## Answer
181215

182216
- We might want to check that the speed is within a safe range.
183-
184-
- We might want to check that the direction is a valid direction. ie not towards a tree, and if so, the car should be applying the brakes.
217+
- We might want to check that the direction is a valid direction. ie not
218+
towards a tree, and if so, the car should be applying the brakes.
185219

186220
:::::::::::::::::::::::::::::::::::::
187221
::::::::::::::::::::::::::::::::::::::::

0 commit comments

Comments
 (0)