blep

Parametrized test with pytest.mark for generator function

May 1st, 2011
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. def pytest_generate_tests(metafunc):
  2.     """Generates tests for test_property_validation & test_mandatory_property.
  3.    """
  4.     if hasattr( metafunc.function, 'parametrized' ):
  5.         args = metafunc.function.parametrized.args
  6.         kwargs = metafunc.function.parametrized.kwargs
  7.         test_generator, args = args[0], args[1:]
  8.         test_generator( metafunc, *args, **kwargs )
  9.  
  10. def _generate_mandatory_property_tests( metafunc, scenarios ):
  11.     for property_value in PROPERTY_AND_VALUES.items():
  12.         name = get_property_class_name( property_value[0] )
  13.         metafunc.addcall(funcargs=dict(property_type_and_data=property_value), id=name)
  14.  
  15. @py.test.mark.parametrized( _generate_mandatory_property_tests, PROPERTY_AND_VALUES )
  16. def test_mandatory_property( property_type_and_data ):
  17.     """Iterates over all property types and check if
  18.       mandatory/optional flags are correctly handled.
  19.    """
  20.     property_type, property_data = property_type_and_data
  21.     property_value = property_data['values'][0]
  22.     mandatory_properties = [property_type( mandatory=True )]
  23.     if not property_data.get('never_pk', False) and not property_data.get('never_mandatory', False):
  24.         mandatory_properties.append( property_type( pk_index=0 ) )
  25.     for mandatory_property in mandatory_properties:
  26.         class DBO(dbo.DBObject):
  27.             mandatory_prop = mandatory_property
  28.             optional_prop = property_type( mandatory=False )
  29.         o = DBO( mandatory_prop=property_value, optional_prop=property_value )
  30.         assert o.mandatory_prop == property_value
  31.         assert o.optional_prop == property_value
  32.         o = DBO( mandatory_prop=property_value, optional_prop=None )
  33.         assert o.mandatory_prop == property_value
  34.         assert o.optional_prop is None
  35.         o = DBO( mandatory_prop=property_value )
  36.         assert o.mandatory_prop == property_value
  37.         assert o.optional_prop is None
  38.         if not property_data.get('never_mandatory', False):
  39.             with py.test.raises(ValueError):
  40.                 DBO()
  41.             with py.test.raises(ValueError):
  42.                 DBO( mandatory_prop=None, optional_prop=None )
  43.             with py.test.raises(ValueError):
  44.                 DBO( mandatory_prop=None )
  45.             with py.test.raises(ValueError):
  46.                 DBO( optional_prop=None )
  47.             with py.test.raises(ValueError):
  48.                 DBO(optional_prop=property_value)
  49.  
  50. def _generate_property_validation_tests( metafunc, value_scenarios_types ):
  51.     for scenario in VALIDATION_SCENARIOS:
  52.         property = scenario['prop']
  53.         class DBO(dbo.DBObject):
  54.             prop = property
  55.         good_value = scenario['good_values'][0]
  56.         base_name = scenario.get( 'name', get_property_class_name(property.__class__) )
  57.         for check_kind, values in [(k, v) for k, v in scenario.items() if k in value_scenarios_types]:
  58.             for index, value in enumerate(values):
  59.                 name = '%s.%s.%d' % (base_name, check_kind, index)
  60.                 metafunc.addcall( id=name, funcargs={
  61.                     'dbo_class': DBO,
  62.                     'value': value,
  63.                     'good_value': good_value} )
  64.  
  65. @py.test.mark.parametrized( _generate_property_validation_tests, ['good_values'] )
  66. def test_property_validation_good_values( dbo_class, value, good_value ):
  67.     """Checks that good values are correctly handled using 'good_values' data."""
  68.     o = dbo_class( prop=value )
  69.     assert o.prop == value
  70.     o = dbo_class()
  71.     o.prop = value
  72.     assert o.prop == value
  73.  
  74. @py.test.mark.parametrized( _generate_property_validation_tests, ['bad_values', 'bad_types'] )
  75. def test_property_validation_bad_values_or_types( dbo_class, value, good_value ):
  76.     """Checks that bad values raise BadPropertyValueError using 'bad_values' and 'bad_types' data."""
  77.     with py.test.raises(dbo.BadPropertyValueError):
  78.         dbo_class( prop=value )
  79.     o = dbo_class( prop=good_value )
  80.     with py.test.raises(dbo.BadPropertyValueError):
  81.         o.prop = value
  82.  
  83. @py.test.mark.parametrized( _generate_property_validation_tests, ['normalize_values'] )
  84. def test_property_validation_normalize_value( dbo_class, value, good_value ):
  85.     """Checks that good values are correctly normalized when a property is set using 'normalize_values' data."""
  86.     o = dbo_class( prop=value[0] )
  87.     assert o.prop == value[1]
  88.     o = dbo_class()
  89.     o.prop = value[0]
  90.     assert o.prop == value[1]
Advertisement
Add Comment
Please, Sign In to add comment