Skip to content

Instantly share code, notes, and snippets.

@diyan
Last active December 19, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diyan/6038238 to your computer and use it in GitHub Desktop.
Save diyan/6038238 to your computer and use it in GitHub Desktop.
Samples how-to use parametrized tests in pyt.test framework.
"""
3 samples how-to use parametrized tests in pyt.test framework:
1. Parametrized fixture with simple string types will generate friendly test name.
2. Parametrized fixture with complex type require you implement __hash__ and __str__ methods. But you will not see friendly test names.
3. The only way to see friendly test names with complex types is to implement pytest_generate_tests hook.
"""
from pytest import fixture
def get_entity_names():
return ['CustomerEntity', 'OrderEntity']
@fixture(params=get_entity_names())
def entity_name(request):
return request.param
def test_simple_parameter_value(entity_name):
assert entity_name
class EntityWithFields(object):
def __init__(self, entity_name=None, filed_names=None):
self.entity_name = entity_name
self.field_names = filed_names
def __str__(self):
return self.entity_name + '__' + '__'.join(self.field_names)
def __hash__(self):
return hash(self.__str__())
def get_entity_with_fields_list():
return [
EntityWithFields('CustomerEntity', ['LoginField', 'AddressField']),
EntityWithFields('OrderEntity', ['LoginField', 'AddressField'])
]
@fixture(params=get_entity_with_fields_list())
def entity_with_fields_auto_name(request):
return request.param
def test_complex_parameter_value_and_auto_name(entity_with_fields_auto_name):
assert entity_with_fields_auto_name
def pytest_generate_tests(metafunc):
if 'entity_with_fields' in metafunc.fixturenames:
entity_with_fields_list = [
EntityWithFields('CustomerEntity', ['LoginField', 'AddressField']),
EntityWithFields('OrderEntity', ['LoginField', 'AddressField'])
]
ids = map(str, entity_with_fields_list)
metafunc.parametrize('entity_with_fields', entity_with_fields_list, ids=ids)
def test_complex_parameter_value_and_manual_name(entity_with_fields):
assert entity_with_fields
"""
$ py.test -v tests/test_parametrized_samples.py
=============================================================================================== test session starts ================================================================================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5 -- /home/diyan/projects/sample_api/bin/python
collected 6 items
tests/test_parametrized_samples.py:13: test_simple_parameter_value[CustomerEntity] PASSED
tests/test_parametrized_samples.py:13: test_simple_parameter_value[OrderEntity] PASSED
tests/test_parametrized_samples.py:41: test_complex_parameter_value_and_auto_name[entity_with_fields_auto_name0] PASSED
tests/test_parametrized_samples.py:41: test_complex_parameter_value_and_auto_name[entity_with_fields_auto_name1] PASSED
tests/test_parametrized_samples.py:56: test_complex_parameter_value_and_manual_name[CustomerEntity__LoginField__AddressField] PASSED
tests/test_parametrized_samples.py:56: test_complex_parameter_value_and_manual_name[OrderEntity__LoginField__AddressField] PASSED
============================================================================================= 6 passed in 0.01 seconds =============================================================================================
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment