Skip to content

Commit

Permalink
Number the docs for each step
Browse files Browse the repository at this point in the history
During the previous tutorial, folks found it hard to navigate
between the steps because the file names did not indicate the
order the steps were in.

Updated mock tests to make use of pytest-mock, which is
the recommended way of mixing mocking and pytest.

Made step 5 and 7 consistent with the rest of the steps.

Added links to solutions, looks like step 7 doesn't have
any solutions for folks to refer to.

Also added a couple pictures to the steps to make them
a bit more fun.
  • Loading branch information
christie committed Jan 31, 2016
1 parent cf49b38 commit 3db2da1
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 57 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# catinabox
[![Build Status](https://travis-ci.org/keeppythonweird/catinabox.svg?branch=master)](https://travis-ci.org/keeppythonweird/catinabox)
[![Coverage Status](https://coveralls.io/repos/keeppythonweird/catinabox/badge.svg?branch=master&service=github)](https://coveralls.io/github/keeppythonweird/catinabox?branch=master)
[![Coverage Status (Pre tests)](https://coveralls.io/repos/keeppythonweird/catinabox/badge.svg?branch=master&service=github)](https://coveralls.io/github/keeppythonweird/catinabox?branch=master)
[![Coverage Status (Post tests)](https://coveralls.io/repos/keeppythonweird/catinabox/badge.svg?branch=solutions&service=github)](https://coveralls.io/github/keeppythonweird/catinabox?branch=solutions)

Intro to Testing and Test Automation in Python

Accompanies the [Intro to Testing and Test Automation in Python slide deck](https://docs.google.com/presentation/d/1Yt-umW3l8NBHqJJk6PbM7tP5n2LZs5iVIyLwvnsGAvA/edit?usp=sharing).
Accompanies the [Intro to Testing and Test Automation in Python slide deck](https://docs.google.com/presentation/d/11j8t75M6lOSZdDv2vJyDKxrk7n5K0GqcX2inx8mzi-M/edit?usp=sharing).

Aesthetic inspired by [@sailorhg](https://twitter.com/sailorhg).

![catinabox](docs/catinabox.png)
![catinabox](pics/catinabox.png)

This repo holds a tutorial which will walk you through adding unit tests,
exploring these features of unit testing in general and pytest in particular:
Expand All @@ -26,14 +28,14 @@ exploring these features of unit testing in general and pytest in particular:

# Tutorial Steps

1. [Setup and run tests](./steps/run_tests.md)
2. [Test a simple function](./steps/simple_function.md)
3. [Create and build a pull request](./steps/pull.md)
4. [Testing incorrect input](./steps/input.md)
5. [Testing classes with fixtures](./steps/classes.md)
6. [Using mock and patch](./steps/mock.md)
7. [Parameterized tests](./steps/params.md)
1. [Setup and run tests](./steps/1-run_tests.md)
2. [Test a simple function](./steps/2-simple_function.md)
3. [Create and build a pull request](./steps/3-pull.md)
4. [Testing incorrect input](./steps/4-input.md)
5. [Testing classes with fixtures](./steps/5-classes.md)
6. [Using mock and patch](./steps/6-mock.md)
7. [Parameterized tests](./steps/7-params.md)

Solutions are visible by viewing the ```solutions``` branch.
Solutions are visible by viewing [the solutions branch](https://github.com/keeppythonweird/catinabox/tree/solutions).

![cattery](docs/cattery.png)
![cattery](pics/cattery.png)
Binary file added pics/calico.png
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
File renamed without changes
Binary file added pics/coveralls.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/fork.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/hero.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/orange.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/time.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions steps/run_tests.md → steps/1-run_tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ your changes, setup your environment and run your tests.
Fork [catinabox](https://github.com/keeppythonweird/catinabox) though
the github webpage, using the `Fork` button.

![fork](../pics/fork.png)

2. Checkout your fork:

```bash
Expand Down
4 changes: 4 additions & 0 deletions steps/simple_function.md → steps/2-simple_function.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This assumes you have setup your environment as described in [run_tests.md]
and that you are in your virtualenv.

![calico](../pics/calico.png)

If you get stuck, [take a peek at the solution](https://github.com/keeppythonweird/catinabox/blob/solutions/tests/test_catmath.py).

1. We will be writing tests for [catmath.py](../catinabox/catmath.py).
This module contains only one function ```cat_years_to_hooman_years```.

Expand Down
6 changes: 4 additions & 2 deletions steps/pull.md → steps/3-pull.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ In this step, you will commit your changes, and create a pull request. This
will kick off continuous integration and coverage reporting, which you can
see from your pull request.

![coveralls](../pics/coveralls.png)

1. Take a look at:
* [The current build status](https://travis-ci.org/keeppythonweird/catinabox)
* [The current coverage](https://coveralls.io/github/keeppythonweird/catinabox?branch=master)

You can see that the build is successfully passing, but the overall coverage
is pretty low.

2. Commit your new unit tests from [the previous step](./simple_function.md)
2. Commit your new unit tests from [the previous step](./2-simple_function.md)
to your fork of `catinabox`:

```bash
Expand All @@ -26,7 +28,7 @@ see from your pull request.

4. Visit github and open a new pull request:

1. [https://github.com/keeppythonweird/catinabox]
1. [https://github.com/keeppythonweird/catinabox](https://github.com/keeppythonweird/catinabox)
2. 'Open Pull Request' or 'Pull Requests' -> 'New Pull Request'

5. As soon as the pull request is created, you will see three checks run:
Expand Down
5 changes: 5 additions & 0 deletions steps/input.md → steps/4-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This assumes you have setup your environment as described in [run_tests.md]
and that you are in your virtualenv.

![orange](../pics/orange.png)

If you get stuck, [take a peek at the solution](https://github.com/keeppythonweird/catinabox/blob/solutions/tests/test_safecatmath.py).

1. This time we will be writing tests for [safecatmath.py](../catinabox/safecatmath.py).
This module contains a new version of ```cat_years_to_hooman_years```.

Expand All @@ -13,6 +17,7 @@ and that you are in your virtualenv.
* ```test__cat_years_to_hooman_years__less_0__raises```
* ```test__cat_years_to_hooman_years__older_than_1000__raises```
* ```test__cat_years_to_hooman_years__string__raises```
* ```test__cat_years_to_hooman_years__nan__raises```

3. Fill in the body of these tests, using ```pytest.raises```.

Expand Down
42 changes: 42 additions & 0 deletions steps/5-classes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Testing Classes with Fixtures

This assumes you have setup your environment as described in [run_tests.md]
and that you are in your virtualenv.

![cattery](../pics/cattery.png)

If you get stuck, [take a peek at the solution](https://github.com/keeppythonweird/catinabox/blob/solutions/tests/test_cattery.py).

1. Refactor the Cattery tests in [test_cattery.py](../tests/test_cattery.py)
to use a py.test fixture.

```python
@pytest.fixture
def cattery_client():
...

def test__add_cats__succeeds(cattery_client):
...
```

2. Try adding a `scope` argument to `pytest.fixture`. What happens when
you try setting it to `scope`, `session`, or `module`?

```python
@pytest.fixture(scope='session')
def cattery_client():
...
```

3. When you are done, run your tests:

```bash
(catpy)user@host:~/catinabox$ python setup.py test
```

4. When the tests run successfully, push them to your pull request:

```bash
(catpy)user@host:~/catinabox$ git commit -a
(catpy)user@host:~/catinabox$ git push origin master
```
4 changes: 4 additions & 0 deletions steps/mock.md → steps/6-mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This assumes you have setup your environment as described in [run_tests.md]
and that you are in your virtualenv.

![time](../pics/time.png)

If you get stuck, [take a peek at the solution](https://github.com/keeppythonweird/catinabox/blob/solutions/tests/test_catactivities.py).

1. Take a look at the ```cat_nap``` method in
[catactivies.py](../catinabox/catactivities.py). This method
sleeps for the provided amount of time if the nap will be
Expand Down
21 changes: 12 additions & 9 deletions steps/params.md → steps/7-params.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# Testing using Parametrization

After each of these steps please feel free to commit your changes, run your tests, and push them to Github!
This assumes you have setup your environment as described in [run_tests.md]
and that you are in your virtualenv.

```bash
$ <Make changes to tests.>
$ python setup.py test
$ git commit -a
$ git push origin master
$ <Check your pull request for Travis build results!>
```
![hero](../pics/hero.png)

1. Try enhancing your tests in `tests/test_catmath.py` to make use of
`pytest.mark.parametrize`.
Expand Down Expand Up @@ -41,7 +36,7 @@ $ <Check your pull request for Travis build results!>
...
```

First we start by parametrizing the fixture.
First we start by parameterizing the fixture.

```python
@pytest.fixture(params=[
Expand All @@ -61,3 +56,11 @@ $ <Check your pull request for Travis build results!>
the tests that require them.

Think of all the testing possibilities!
```
4. When the tests run successfully, push them to your pull request:
```bash
(catpy)user@host:~/catinabox$ git commit -a
(catpy)user@host:~/catinabox$ git push origin master
```
22 changes: 0 additions & 22 deletions steps/classes.md

This file was deleted.

2 changes: 1 addition & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mccabe==0.3.1
mock
pyflakes==1.0.0
pytest-cov==2.1.0
pytest-flakes==1.0.1
pytest-mccabe==0.1
pytest-pep8==1.0.6
pytest-mock==0.10.1
pytest==2.8.2
six==1.9.0
16 changes: 5 additions & 11 deletions tests/test_catactivities.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
# import pytest
import six
import time
# import time

# from catinabox import catactivities

if six.PY2:
import mock
else:
from unittest import mock


@mock.patch.object(time, "sleep", autospec=True)
def test__cat_nap__satisfying_nap(sleep):
def test__cat_nap__satisfying_nap(mocker):
# mock_sleep = mocker.patch.object(time, "sleep", autospec=True)
assert True


@mock.patch.object(time, "sleep", autospec=True)
def test__cat_nap__not_satisfying(sleep):
def test__cat_nap__not_satisfying(mocker):
# mock_sleep = mocker.patch.object(time, "sleep", autospec=True)
assert True
5 changes: 5 additions & 0 deletions tests/test_safecatmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ def test__cat_years_to_hooman_years__older_than_1000__raises():

def test__cat_years_to_hooman_years__string__raises():
assert True


def test__cat_years_to_hooman_years__nan__raises():
# hooman_age = float('nan')
assert True

0 comments on commit 3db2da1

Please sign in to comment.