Introduction
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 pytest.py
i.e. first_test.py
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 first_test.py
That should make following output:
pytest -rs first_pytest.py
============================================================================================================ 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
first_pytest.py ..
========================================================================================================= 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
@pytest.fixture(
scope='module',
params=[
'bikingendorphines/example_data/15212277.gpx',
'bikingendorphines/example_data/15212277.gpx'
]
)
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():
points.append(point)
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 :)
Update : Yes :) Travis accepted my changes and created pyreverse output in gh-pages, see GH_PAGE, Classes as PNG File
Tools and applications used:
- VIM + Plugins (Fugitive, Vim-Markdown and Vimux )
- Docker
- Tmux
LL (Lessons Learned)
1. Don't try to name your file with test-cases a pytest.py
! :)
If your filename will have name pytest.py
as I did for my example of usage pytest, I've found python mismatched pytest and was confused.
Error like this popup:
________________________________________________________________________________________________________ ERROR collecting pytest.py _________________________________________________________________________________________________________
import file mismatch:
imported module 'pytest' has this __file__ attribute:
/home/bawi/projects/deployed/anselmos_blog/data/content/examples/pytest/.env/local/lib/python2.7/site-packages/pytest.py
which is not the same as the test file we want to collect:
/home/bawi/projects/deployed/anselmos_blog/data/content/examples/pytest/pytest.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
Solution:
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.
Comments
comments powered by Disqus