1
0
Fork 0
mirror of https://github.com/veekun/pokedex.git synced 2024-08-20 18:16:34 +00:00

Modernize our use of py.test

This commit updates the tests to take advantage of some of py.test's
newer features.  Requires py.test 2.3 or newer.  Tested with 2.3.0 and
2.5.2.

Tests which were parametrized now use py.test's built-in
parametrization[1].

The session and lookup objects are now implemented as fixtures[2].
The media root is a fixture as well.  Fixtures are automatically passed
to any function that expects them.

Since the session is now created in one place, it is now possible to
provide an engine URI on the command line when running py.test.  Ditto
for the index directory.  (But the environment variables still work of
course.)

Slow tests are now marked as such and not run unless the --all option is
given.

A couple media tests are marked as xfail (expected to fail) because they
are broken.

[1]: http://pytest.org/latest/parametrize.html
[2]: http://pytest.org/latest/fixture.html
This commit is contained in:
Andrew Ekstedt 2014-07-06 20:25:04 -07:00
parent 29824c73f4
commit 0094e9584c
10 changed files with 285 additions and 299 deletions

View file

@ -1,4 +1,3 @@
# Configuration for the tests.
# Use `py.test` to run the tests.
@ -8,14 +7,37 @@ import pytest
import os
def pytest_addoption(parser):
parser.addoption("--media-root", action="store",
default=None,
group = parser.getgroup("pokedex")
group.addoption("--engine", action="store", default=None,
help="Pokedex database URI")
group.addoption("--index", action="store", default=None,
help="Path to index directory")
group.addoption("--media-root", action="store", default=None,
help="Root for the media files (if not specified and pokedex/data/media doesn't exist, tests are skipped)")
parser.addoption("--all", action="store_true", default=False,
group.addoption("--all", action="store_true", default=False,
help="Run all tests, even those that take a lot of time")
def pytest_generate_tests(metafunc):
for funcargs in getattr(metafunc.function, 'funcarglist', ()):
metafunc.addcall(funcargs=funcargs)
for posargs in getattr(metafunc.function, 'posarglist', ()):
metafunc.addcall(funcargs=dict(zip(metafunc.funcargnames, posargs)))
def pytest_runtest_setup(item):
if 'slow' in item.keywords and not item.config.getvalue('all'):
pytest.skip("skipping slow tests")
@pytest.fixture(scope="module")
def session(request):
import pokedex.db
engine_uri = request.config.getvalue("engine")
return pokedex.db.connect(engine_uri)
@pytest.fixture(scope="module")
def lookup(request, session):
import pokedex.lookup
index_dir = request.config.getvalue("index")
return pokedex.lookup.PokedexLookup(index_dir, session)
@pytest.fixture(scope="session")
def media_root(request):
media_root = request.config.getvalue("media_root")
if not media_root:
media_root = os.path.join(os.path.dirname(__file__), '..', 'data', 'media')
if not os.path.isdir(media_root):
raise pytest.skip("Media unavailable")
return media_root