Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions episodes/01-why-test-my-code.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ for the `add` function and call it `test_add`:

```python
def test_add():
# Check that it adds two positive integers
if add(1, 2) != 3:
print("Test failed!")
# Check that it adds zero
if add(5, 0) != 5:
print("Test failed!")
# Check that it adds two negative integers
if add(-1, -2) != -3:
print("Test failed!")
# Check that it adds two positive integers
if add(1, 2) != 3:
print("Test failed!")
# Check that it adds zero
if add(5, 0) != 5:
print("Test failed!")
# Check that it adds two negative integers
if add(-1, -2) != -3:
print("Test failed!")
```

Here we check that the function works for a set of test cases. We ensure that
Expand All @@ -98,7 +98,7 @@ functions:

```python
def greet_user(name):
return "Hello" + name + "!"
return "Hello" + name + "!"
```

```python
Expand All @@ -116,8 +116,8 @@ working as expected:

```python
def test_greet_user():
if greet_user("Alice") != "Hello Alice!":
print("Test failed!")
if greet_user("Alice") != "Hello Alice!":
print("Test failed!")
```

The second function will crash if `x2 - x1` is zero.
Expand All @@ -127,21 +127,21 @@ unexpected behaviour:

```python
def test_gradient():
if gradient(1, 1, 2, 2) != 1:
print("Test failed!")
if gradient(1, 1, 2, 3) != 2:
print("Test failed!")
if gradient(1, 1, 1, 2) != "Undefined":
print("Test failed!")
if gradient(1, 1, 2, 2) != 1:
print("Test failed!")
if gradient(1, 1, 2, 3) != 2:
print("Test failed!")
if gradient(1, 1, 1, 2) != "Undefined":
print("Test failed!")
```

And we could have amended the function:

```python
def gradient(x1, y1, x2, y2):
if x2 - x1 == 0:
return "Undefined"
return (y2 - y1) / (x2 - x1)
if x2 - x1 == 0:
return "Undefined"
return (y2 - y1) / (x2 - x1)
```

:::::::::::::::::::::::::::::::::
Expand All @@ -156,13 +156,13 @@ consider the following function:
```python

def multiply(a, b):
return a * a
return a * a

def divide(a, b):
return a / b
return a / b

def triangle_area(base, height):
return divide(multiply(base, height), 2)
return divide(multiply(base, height), 2)
```

There is a bug in this code too, but since we have several functions calling
Expand Down Expand Up @@ -205,7 +205,7 @@ Consider a function that controls a driverless car.
```python
def drive_car(speed, direction):

... # complex car driving code
... # complex car driving code

return speed, direction, brake_status
```
Expand Down
34 changes: 17 additions & 17 deletions episodes/02-simple-tests.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ project_directory/

```python
def add(a, b):
return a + b
return a + b
```

- And in `test_calculator.py`, write the test for the add function that we
Expand All @@ -61,14 +61,14 @@ def add(a, b):
from calculator import add

def test_add():
# Check that it adds two positive integers
assert add(1, 2) == 3
# Check that it adds two positive integers
assert add(1, 2) == 3

# Check that it adds zero
assert add(5, 0) == 5
# Check that it adds zero
assert add(5, 0) == 5

# Check that it adds two negative integers
assert add(-1, -2) == -3
# Check that it adds two negative integers
assert add(-1, -2) == -3
```

The `assert` statement will crash the test by raising an `AssertionError` if
Expand All @@ -88,7 +88,7 @@ Now, let's run the test. We can do this by running the following command in the
(make sure you're in the `my_project` directory before running this command)

```bash
❯ pytest ./
❯ pytest
```

This command tells Pytest to run all the tests in the current directory.
Expand Down Expand Up @@ -118,7 +118,7 @@ multiplies two numbers together.

```python
def multiply(a, b):
return a * b
return a * b
```

- Then write a test for this function in `test_calculator.py`. Remember to
Expand All @@ -137,14 +137,14 @@ could look like this:

```python
def test_multiply():
# Check that positive numbers work
assert multiply(5, 5) == 25
# Check that multiplying by 1 works
assert multiply(1, 5) == 5
# Check that multiplying by 0 works
assert multiply(0, 3) == 0
# Check that negative numbers work
assert multiply(-5, 2) == -10
# Check that positive numbers work
assert multiply(5, 5) == 25
# Check that multiplying by 1 works
assert multiply(1, 5) == 5
# Check that multiplying by 0 works
assert multiply(0, 3) == 0
# Check that negative numbers work
assert multiply(-5, 2) == -10
```

:::::::::::::::::::::::::::::::::
Expand Down
8 changes: 4 additions & 4 deletions episodes/04-unit-tests-best-practices.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ def randomly_sample_and_filter_participants(
min_height: int,
max_height: int
):
"""Participants is a list of tuples, containing the age and height of each participant
"""Participants is a list of dicts, containing the age and height of each participant
participants = [
{age: 25, height: 180},
{age: 30, height: 170},
{age: 35, height: 160},
{age: 25, height: 180},
{age: 30, height: 170},
{age: 35, height: 160},
]
"""

Expand Down
8 changes: 3 additions & 5 deletions episodes/05-testing-exceptions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ Take this example of the `square_root` function. We don't have time to implement
```python

def square_root(x):
if x < 0:
raise ValueError("Cannot compute square root of negative number yet!")
return x ** 0.5

if x < 0:
raise ValueError("Cannot compute square root of negative number yet!")
return x ** 0.5
```

We can test that the function raises an exception using `pytest.raises` as follows:
Expand All @@ -52,7 +51,6 @@ def test_square_root():
with pytest.raises(ValueError) as e:
square_root(-1)
assert str(e.value) == "Cannot compute square root of negative number yet!"

```

::::::::::::::::::::::::::::::::::::: challenge
Expand Down
Loading
Loading