Skip to content

Content fixes #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This tutorial helps you to learn automated testing in Python 3 using the `pytest

*Captain Ahab was vicious because Moby Dick, the white whale, had bitten off his leg. So the captain set sail for a hunt. For months he was searching the sea for the white whale. The captain finally attacked the whale with a harpoon. Unimpressed, the whale devoured captain, crew and ship. The whale won.*

![tick marks while counting words](../images/counting470.png "Counting words")
![tick marks while counting words](images/counting470.png "Counting words")

Herman Melville's book *“Moby Dick”* describes the epic fight between the captain of a whaling ship and a whale. In the book, the whale wins by eating most of the other characters. **But does he also win by being mentioned more often?**

Expand All @@ -19,13 +19,19 @@ Herman Melville's book *“Moby Dick”* describes the epic fight between the ca

clone the repository:

:::bash
git clone https://github.com/krother/python_testing_tutorial.git
```bash
git clone https://github.com/krother/python_testing_tutorial.git
```

install **pytest**:

:::bash
pip install pytest
```bash
pip install pytest
```

## Getting Started

Start with the first exercises in the chapter [Unit Tests](articles/unit_tests.md)!

## Chapters

Expand Down Expand Up @@ -65,4 +71,4 @@ Attribution License 4.0.

## Contributors

Kristian Rother, Magdalena Rother, Daniel Szoska
Kristian Rother, Magdalena Rother, Daniel Szoska, Malte Bonart
53 changes: 0 additions & 53 deletions articles/challenges.md

This file was deleted.

29 changes: 16 additions & 13 deletions articles/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ There, add a function that loads the file `data/mobydick_summary.txt`:

Place the decorator `@pytest.fixture` on top of it:

:::python3
import pytest
```python
import pytest

@pytest.fixture
def text_summary():
return open(...).read()
@pytest.fixture
def text_summary():
return open(...).read()
```

----

### Exercise 2: Using the fixture

Now create a module `test_corpus.py` with a function that uses the fixture:

:::python3
def test_short_sample(text_summary):
assert count_words(text_summary) == 77
```python
def test_short_sample(text_summary):
assert count_words(text_summary) == 77
```

Execute the module with `pytest`. Note that you **do not** need to import `conftest`. Pytest does that automatically.

Expand All @@ -44,11 +46,12 @@ Create a fixture for the full text of the book `mobydick_full.txt` as well.

Create a fixture in `conftest.py` that prepares a dictionary with word counts using the `word_counter.count_words_dict()` function.

:::python3
from word_counter import count_words_dict
```python
from word_counter import count_words_dict

@pytest.fixture
def count_dict(text_summary):
return ...
@pytest.fixture
def count_dict(text_summary):
return ...
```

Write a simple test that makes sure the dictionary is not empty.
8 changes: 0 additions & 8 deletions articles/mock_objects.md

This file was deleted.

20 changes: 0 additions & 20 deletions articles/multiple_packages.md

This file was deleted.

38 changes: 22 additions & 16 deletions articles/organizing_tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,44 @@ Make sure the name of the class starts with the word `Test`.
Indent your test functions so that they belong to the class.
Add `self` as the first parameter of each function:

:::python3
class TestDummy:
```python
class TestDummy:

def test_dummy(self):
assert ...
def test_dummy(self):
assert ...
```

----

### Exercise 2: Test collection

Run all tests written so far by simply typing

:::bash
pytest
```bash
python -m pytest
```

----

### Exercise 3: Test selection

Run only one test file:

:::bash
pytest FILE_NAME
```bash
python -m pytest FILE_NAME
```

Run only one test class:

:::bash
pytest FILE_NAME::CLASS_NAME
```bash
pytest -m pytest FILE_NAME::CLASS_NAME
```

Finally, run a single test:

:::bash
pytest FILE_NAME::CLASS_NAME::TEST_NAME
```bash
pytest -m pytest FILE_NAME::CLASS_NAME::TEST_NAME
```

----

Expand All @@ -51,7 +56,8 @@ Find out which options of pytest do the following:

*more verbose output | re-run failing tests | stop on first test that fails*

:::bash
pytest -lf
pytest -v
pytest -x
```bash
pytest --lf
pytest -v
pytest -x
```
30 changes: 16 additions & 14 deletions articles/parameterized.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

The tests in `test_parameterized.py` check a list of pairs (word, count) that apply to the text file `mobydick_summary.txt`:

:::python3
PAIRS = [
('whale', 5),
('goldfish', 0),
('captain', 4),
('white', 2),
('jellyfish', 99),
('harpoon', 1),
]
```python
PAIRS = [
('whale', 5),
('goldfish', 0),
('captain', 4),
('white', 2),
('jellyfish', 99),
('harpoon', 1),
]
```

Run the tests and see what happens.

Expand All @@ -34,12 +35,13 @@ We will create six tests from the example data.
Use the **test parametrization in pytest**.
Change the test function by adding the following decorator:

:::python3
import pytest
```python
import pytest

@pytest.mark.parametrize('word, number', PAIRS)
def test_count_words_dict(word, number):
...
@pytest.mark.parametrize('word, number', PAIRS)
def test_count_words_dict(word, number):
...
```

The two arguments will be filled in automatically.
Now remove the `for` loop.
Expand Down
15 changes: 9 additions & 6 deletions articles/test_coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

For the next exercises, you need to install a small plugin:

:::bash
pip install pytest-cov
```bash
pip install pytest-cov
```

----

### Exercise 1: Calculate Test Coverage

Calculate the percentage of code covered by automatic tests:

:::bash
pytest --cov=.
```bash
python -m pytest --cov=.
```

Instead of the `.` you can insert the path you would like to see in the coverage report.

Expand All @@ -24,8 +26,9 @@ Check whether any hidden files have appeared.
### Exercise 2: Identify uncovered lines
Find out which lines are not covered by tests. Execute

:::bash
coverage html
```bash
python -m pytest --cov=. --cov-report term-missing
```

Open the resulting file `htmlcov/index.html` in a web browser.

Expand Down
24 changes: 14 additions & 10 deletions articles/unit_tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@

#### How many words are in the following sentence?

:::bash
Call me Ishmael.
```bash
Call me Ishmael.
```

----

#### How many words are in the next sentence?

:::bash
"you haint no objections to sharing a harpooneer's blanket,
have ye? I s'pose you are goin' a-whalin',
so you'd better get used to that sort of thing."
```bash
"you haint no objections to sharing a harpooneer's blanket,
have ye? I s'pose you are goin' a-whalin',
so you'd better get used to that sort of thing."
```


----
Expand All @@ -26,15 +28,17 @@ The function `count_words()` in the module **word_counter.py** calculates the nu

For instance, we would expect the following input to result in a word count of `3`:

:::bash
Call me Ishmael
```bash
Call me Ishmael
```

Your task is to prove that the `count_words()` function in fact returns `3`.

Run the example test in `test_unit_test.py` with

:::bash
pytest test_unit_test.py
```bash
python -m pytest test/test_unit_test.py
```

----

Expand Down
Binary file removed cover.png
Binary file not shown.
File renamed without changes
Binary file modified images/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading