Skip to content

Commit 717df1a

Browse files
Merge pull request #21 from ResearchCodingClub/parametrization_tweaks
Tweaks to parametrization
2 parents 90fb03c + 5b4d59f commit 717df1a

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

episodes/06-floating-point-data.Rmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ places. This can be affected by:
3232

3333
- Choice of algorithm.
3434
- Precise order of operations.
35-
- Order in which parallel processes finish.
3635
- Inherent randomness in the calculation.
3736

3837
We could therefore test our code using `assert result == 2.34958124890e-31`,
@@ -54,8 +53,8 @@ it is best to use a _relative_ tolerance:
5453
from math import fabs
5554

5655
def test_float_rtol():
57-
actual = my_function()
5856
expected = 7.31926e12 # Reference solution
57+
actual = my_function()
5958
rtol = 1e-3
6059
# Use fabs to ensure a positive result!
6160
assert fabs((actual - expected) / expected) < rtol
@@ -69,8 +68,8 @@ tolerance:
6968
from math import fabs
7069

7170
def test_float_atol():
72-
actual = my_function()
7371
expected = 0.0 # Reference solution
72+
actual = my_function()
7473
atol = 1e-5
7574
# Use fabs to ensure a positive result!
7675
assert fabs(actual - expected) < atol
@@ -90,6 +89,10 @@ inefficiently!).
9089
import random
9190

9291
def estimate_pi(iterations):
92+
"""
93+
Estimate pi by counting the number of random points
94+
inside a quarter circle of radius 1
95+
"""
9396
num_inside = 0
9497
for _ in range(iterations):
9598
x = random.random()

episodes/08-parametrization.Rmd

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ This test is quite long and repetitive. We can use parametrization to make it mo
9898
import pytest
9999

100100
@pytest.mark.parametrize(
101-
("p1x, p1y, p2x, p2y, p3x, p3y, expected"),
101+
"p1x, p1y, p2x, p2y, p3x, p3y, expected",
102102
[
103-
pytest.param(0, 0, 2, 0, 1, 1.7320, 1.7320, id="Equilateral triangle"),
103+
pytest.param(0, 0, 2, 0, 1, 1.732, 1.732, id="Equilateral triangle"),
104104
pytest.param(0, 0, 3, 0, 0, 4, 6, id="Right-angled triangle"),
105105
pytest.param(0, 0, 4, 0, 2, 8, 16, id="Isosceles triangle"),
106106
pytest.param(0, 0, 3, 0, 1, 4, 6, id="Scalene triangle"),
@@ -119,13 +119,14 @@ Let's have a look at how this works.
119119

120120
Similar to how fixtures are defined, the `@pytest.mark.parametrize` line is a decorator, letting pytest know that this is a parametrized test.
121121

122-
- The first argument is a tuple, a list of the names of the parameters you want to use in your test. For example
123-
`("p1x, p2y, p2x, p2y, p3x, p3y, expected")` means that we will use the parameters `p1x`, `p1y`, `p2x`, `p2y`, `p3x`, `p3y` and `expected` in our test.
122+
- The first argument is a string listing the names of the parameters you want to use in your test. For example
123+
`"p1x, p2y, p2x, p2y, p3x, p3y, expected"` means that we will use the parameters `p1x`, `p1y`, `p2x`, `p2y`, `p3x`, `p3y` and `expected` in our test.
124124

125-
- The second argument is a list of `pytest.param` objects. Each `pytest.param` object is a tuple of the values you want to test, with an optional `id` argument to give a name to the test.
126-
For example, `pytest.param(0, 0, 2, 0, 1, 1.7320, 6, id="Equilateral triangle")` means that we will test the function with the parameters `0, 0, 2, 0, 1, 1.7320, 6` and give it the name "Equilateral triangle".
125+
- The second argument is a list of `pytest.param` objects. Each `pytest.param` object contains the values you want to test, with an optional `id` argument to give a name to the test.
127126

128-
(note that if the test fails you will see the id in the output, so it's useful to give them meaningful names to help you understand what went wrong.)
127+
For example, `pytest.param(0, 0, 2, 0, 1, 1.732, 1.732, id="Equilateral triangle")` means that we will test the function with the parameters `0, 0, 2, 0, 1, 1.732, 1.732` and give it the name "Equilateral triangle".
128+
129+
Note that if the test fails you will see the id in the output, so it's useful to give them meaningful names to help you understand what went wrong.
129130

130131
- The test function will be run once for each set of parameters in the list.
131132

@@ -135,7 +136,7 @@ This is a much more concise way to write tests for functions that need to be tes
135136

136137
::::::::::::::::::::::::::::::::::::: challenge
137138

138-
## Challenge - Practice with Parametrization
139+
## Practice with Parametrization
139140

140141
Add the following function to `advanced/advanced_calculator.py` and write a parametrized test for it in `tests/test_advanced_calculator.py` that tests the function with a range of different inputs
141142
using parametrization.
@@ -158,7 +159,7 @@ def is_prime(n: int) -> bool:
158159
import pytest
159160

160161
@pytest.mark.parametrize(
161-
("n, expected"),
162+
"n, expected",
162163
[
163164
pytest.param(0, False, id="0 is not prime"),
164165
pytest.param(1, False, id="1 is not prime"),

0 commit comments

Comments
 (0)