Skip to content

Commit c83f537

Browse files
authored
Merge pull request #16 from gjbex/development
Replace slides on unittest by slides on pytest
2 parents 5e605eb + c97e91a commit c83f537

File tree

11 files changed

+134
-1
lines changed

11 files changed

+134
-1
lines changed

python_software_engineering.pptx

3.17 KB
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Markers
2+
3+
pytest supports custom markers that can be used to select or deselect tests.
4+
5+
6+
## What is it?
7+
8+
1. `fibonacci.py`: Python script that defines a function to compute the
9+
Fibonacci sequence in a (very) naive way.
10+
1. `test_fibonacci.py`: test script that uses pytest to test the
11+
implementation of the Fibonacci sequence computation. One of the tests has been marked with the `slow` marker.
12+
1. `pytest.ini`: configuration file for pytest that defines the markers that
13+
can be used in the test scripts.
14+
15+
16+
## How to use it?
17+
18+
To run all tests:
19+
```bash
20+
$ pytest
21+
```
22+
23+
To run all tests except the `slow` test:
24+
```bash
25+
$ pytest -m "not slow"
26+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
3+
def fib(n):
4+
if n < 0:
5+
raise ValueError('fac argument must be positive')
6+
elif n < 2:
7+
return 1
8+
else:
9+
return fib(n - 1) + fib(n - 2)
10+
11+
12+
if __name__ == '__main__':
13+
from argparse import ArgumentParser
14+
arg_parser = ArgumentParser(description='compute fibonacci numbers')
15+
arg_parser.add_argument('n', type=int, default=5,
16+
help='maximum argument')
17+
options = arg_parser.parse_args()
18+
for i in range(options.n + 1):
19+
print(f'fib({i}) = {fib(i)}')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
markers =
3+
slow: marks tests as slow (deselect with '-m "not slow"')
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fibonacci import fib
2+
3+
import pytest
4+
5+
6+
def test_fib_0():
7+
assert fib(0) == 1
8+
9+
def test_fib_1():
10+
assert fib(1) == 1
11+
12+
def test_fib_2():
13+
assert fib(2) == 2
14+
15+
def test_fib_5():
16+
assert fib(5) == 8
17+
18+
@pytest.mark.slow
19+
def test_fib_40():
20+
assert fib(40) == 165580141
21+
22+
def test_fib_negative():
23+
with pytest.raises(ValueError):
24+
_ = fib(-1)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Parameterize
2+
3+
pytest tests can be parameterized so that it can be run on a list of input
4+
arguments and expected output.
5+
6+
7+
## What is it?
8+
9+
1. `fibonacci.py`: Python script that implements the Fibonacci function.
10+
1. `test_fib.py`: parameterized tests for the Fibonacci function.
11+
1. `pytest.ini`: pytest configuration file that defines a `slow` marker.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
3+
def fib(n):
4+
if n < 0:
5+
raise ValueError('fac argument must be positive')
6+
elif n < 2:
7+
return 1
8+
else:
9+
return fib(n - 1) + fib(n - 2)
10+
11+
12+
if __name__ == '__main__':
13+
from argparse import ArgumentParser
14+
arg_parser = ArgumentParser(description='compute fibonacci numbers')
15+
arg_parser.add_argument('n', type=int, default=5,
16+
help='maximum argument')
17+
options = arg_parser.parse_args()
18+
for i in range(options.n + 1):
19+
print(f'fib({i}) = {fib(i)}')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
markers =
3+
slow: marks tests as slow (deselect with '-m "not slow"')
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from fibonacci import fib
2+
3+
import pytest
4+
5+
@pytest.mark.parametrize("n, expected", [
6+
(0, 1),
7+
(1, 1),
8+
(2, 2),
9+
(5, 8),
10+
])
11+
def test_fib(n, expected):
12+
assert fib(n) == expected
13+
14+
@pytest.mark.slow
15+
@pytest.mark.parametrize("n, expected", [
16+
(40, 165580141),
17+
(41, 267914296),
18+
(42, 433494437),
19+
])
20+
def test_fib_40(n, expected):
21+
assert fib(n) == expected
22+
23+
def test_fib_negative():
24+
with pytest.raises(ValueError):
25+
_ = fib(-1)

source-code/testing/PyTest/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ module provided by the Python standard library.
66
1. `Simple`: a very simple illustration of how to test using `pytest`.
77
1. `Cython`: testing a Cython implementation using `pytest`.
88
1. `Fixtures`: illustrates using fixtures with pytest.
9+
1. `CustomMarkers`: illustrates using custom markers with pytest to
10+
selectively run tests.
11+
1. `Parametrize`: illustrates using parameterized tests.

0 commit comments

Comments
 (0)