So my first initial change for Parameterizing test-cases was a bit of failure - you can check this Parameterized Django Testcases At Biking Endorphines

Let's change that :)

Prerequisites - How to use Pytests

1. Install pytests with pip :

pip install pytest

2. Import Pytest in your file containing tests:

import pytest

3. But how do you actually use it ?

That's a good question.

First create a file with different name then i.e. and put similar content as I've created :

Pytests example
import pytest

def func(num):
    Simple adding function for pytest showcase
    return num + 1

def test_answer_fail():
    Test for checking func with negative values
    with pytest.raises(AssertionError):
        assert func(3) == 5

def test_answer_success():
    Test for checking func with possitive values
    assert func(4) == 5

Then use following bash command:

pytest -rs

That should make following output:

pytest -rs 
============================================================================================================ test session starts ============================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/bawi/projects/deployed/anselmos_blog/data/content/examples/pytest, inifile:
collected 2 items ..

========================================================================================================= 2 passed in 0.02 seconds ==========================================================================================================

What changes I've made in code - a diff between code-commits using asciinema and vim-diff tool?

1. I've followed Good Integration Practices

What that means ? I've figure-out that this tests that I intent to create are out-side of django scope, so why not just put them literally out-side?

That's why I've created tests directory at directory bikingendorphines/tests and put all my test-files in there.

2. Test-Case itself.

Now I've change code so it looks like this :

Tests for GPXReader class

import pytest

from web.utils import GPXReader

def gpxreader(request):
    Creates GPXReader instance with predefined gpx file
    return GPXReader(request.param)

# pylint: disable=redefined-outer-name
def test_get_points(gpxreader):
    Tests if there will be data output from get_points.

    Test if:
    - points exists for different types.
    - what will happen if types exists, but no data?
    points = []
    for point in gpxreader.get_points():
    assert len(points) > 0

Final result

Check how much my code changed in this commit that shows all diffs:

Adds pytest. Adds GPXReader pytest-alike test

Code commits done for this post:

That's the same commit that changes GPXReader:

Adds pytest. Adds GPXReader pytest-alike test

And I've finally Squashed and Merged the gpx-solution as a initial solution.

Now I hope Travis will make my day better and confirm pylint&unittest as a good-to-go and create Pyreverse GH PAGE :)

Check results of Travis here

Update : Yes :) Travis accepted my changes and created pyreverse output in gh-pages, see GH_PAGE, Classes as PNG File

Tools and applications used:

LL (Lessons Learned)

1. Don't try to name your file with test-cases a ! :)

If your filename will have name as I did for my example of usage pytest, I've found python mismatched pytest and was confused.

Error like this popup:

________________________________________________________________________________________________________ ERROR collecting _________________________________________________________________________________________________________
import file mismatch:
imported module 'pytest' has this __file__ attribute:
which is not the same as the test file we want to collect:
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules


You may overcome this issue change your name to something different than name of the library itself :)

2. Create plan and stick to it

That's more a Tips&Tricks then Lessons Learned, but it may also count as LL. I've figure-out finally how to manage the planning - I'll try my method and let you know if it will actually work:

  • Create a .md file for article
  • Make only main parts information of article that will briefly describe a kind-of-todo
  • Make all of this parts and describe them more deeply.



1. Change behaviour of GPXReaderTestCase to more pytest alike and add some more test code

2. Push finally first version of GPX Reader to master via pull-request.

What's next

1. Add more tests to GPXReader test cases.

2. Add more business logic to GPXReader - endorphines-algorithms :)


comments powered by Disqus