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
pokedex/tests

View file

@ -1,12 +1,11 @@
# Encoding: UTF-8
from pokedex.tests import *
import pytest
parametrize = pytest.mark.parametrize
from pokedex.lookup import PokedexLookup
lookup = PokedexLookup()
@positional_params(
@parametrize(
('input', 'table', 'id'),
[
# Simple lookups
(u'Eevee', 'pokemon_species',133),
(u'Scratch', 'moves', 10),
@ -31,8 +30,9 @@ lookup = PokedexLookup()
(u'이브이', 'pokemon_species', 133),
(u'伊布', 'pokemon_species', 133),
(u'Evoli', 'pokemon_species', 133),
)
def test_exact_lookup(input, table, id):
]
)
def test_exact_lookup(lookup, input, table, id):
results = lookup.lookup(input)
assert len(results) == 1
assert results[0].exact == True
@ -42,19 +42,19 @@ def test_exact_lookup(input, table, id):
assert row.id == id
def test_id_lookup():
def test_id_lookup(lookup):
results = lookup.lookup(u'1')
assert len(results) >= 5
assert all(result.object.id == 1 for result in results)
def test_multi_lookup():
def test_multi_lookup(lookup):
results = lookup.lookup(u'Metronome')
assert len(results) == 2
assert results[0].exact
def test_type_lookup():
def test_type_lookup(lookup):
results = lookup.lookup(u'pokemon:1')
assert results[0].object.__tablename__ == 'pokemon_species'
assert len(results) == 1
@ -64,7 +64,7 @@ def test_type_lookup():
assert results[0].object.name == u'Bulbasaur'
def test_language_lookup():
def test_language_lookup(lookup):
# There are two objects named "charge": the move Charge, and the move
# Tackle, which is called "Charge" in French.
results = lookup.lookup(u'charge')
@ -85,7 +85,9 @@ def test_language_lookup():
assert results[0].object.name, u'Tackle'
@positional_params(
@parametrize(
('misspelling', 'name'),
[
# Regular English names
(u'chamander', u'Charmander'),
(u'pokeball', u'Poké Ball'),
@ -97,51 +99,58 @@ def test_language_lookup():
# Sufficiently long foreign names
(u'カクレオ', u'Kecleon'),
(u'Yamikrasu', u'Murkrow'),
)
def test_fuzzy_lookup(misspelling, name):
]
)
def test_fuzzy_lookup(lookup, misspelling, name):
results = lookup.lookup(misspelling)
first_result = results[0]
assert first_result.object.name == name
def test_nidoran():
def test_nidoran(lookup):
results = lookup.lookup(u'Nidoran')
top_names = [result.object.name for result in results[0:2]]
assert u'Nidoran♂' in top_names
assert u'Nidoran♀' in top_names
@positional_params(
@parametrize(
('wildcard', 'name'),
[
(u'pokemon:*meleon', u'Charmeleon'),
(u'item:master*', u'Master Ball'),
(u'ee?ee', u'Eevee'),
)
def test_wildcard_lookup(wildcard, name):
]
)
def test_wildcard_lookup(lookup, wildcard, name):
results = lookup.lookup(wildcard)
first_result = results[0]
assert first_result.object.name == name
def test_bare_random():
def test_bare_random(lookup):
for i in range(5):
results = lookup.lookup(u'random')
assert len(results) == 1
@positional_params(
[u'pokemon_species'],
[u'moves'],
[u'items'],
[u'abilities'],
[u'types'],
)
def test_qualified_random(table_name):
@parametrize(
'table_name',
[
u'pokemon_species',
u'moves',
u'items',
u'abilities',
u'types'
]
)
def test_qualified_random(lookup, table_name):
results = lookup.lookup(u'random', valid_types=[table_name])
assert len(results) == 1
assert results[0].object.__tablename__ == table_name
def test_crash_empty_prefix():
def test_crash_empty_prefix(lookup):
"""Searching for ':foo' used to crash, augh!"""
results = lookup.lookup(u':Eevee')
assert results[0].object.name == u'Eevee'