def test_with_custom_type(self): hex_int = lambda x: int(x, 16) test_validator = validators.InRange('0x10', '0x12', type=hex_int) self.assertTrue(test_validator(0x11)) self.assertFalse(test_validator(0x9)) self.assertEqual(test_validator.minimum, 0x10) self.assertEqual(test_validator.maximum, 0x12)
def test_comparable_with_equivalent_in_range_validator(self): validator_a = validators.InRange(minimum=0, maximum=10) validator_b = validators.InRange(minimum=0, maximum=10) self.assertEqual(validator_a, validator_b) validator_c = validators.InRange(maximum=10) self.assertNotEqual(validator_a, validator_c)
def test_str_does_not_raise(self): for args in [(None, 10), (0, None), (0, 10), (5, 5)]: test_validator = validators.InRange(*args) str(test_validator) # Check that we constructed a usable validator. self.assertTrue(test_validator(5))
def test_upper_bound_validator(self): test_validator = validators.InRange(maximum=10) for valid_value in [-float('inf'), -10, 0, 10]: self.assertTrue(test_validator(valid_value)) for invalid_value in [11, float('inf')]: self.assertFalse(test_validator(invalid_value))
def test_lower_bound_validator(self): test_validator = validators.InRange(minimum=-10) for valid_value in [-10, 0, 10, float('inf')]: self.assertTrue(test_validator(valid_value)) for invalid_value in [-float('inf'), -11]: self.assertFalse(test_validator(invalid_value))
def test_invalidates_non_numbers(self): self.assertFalse(validators.InRange(0, 10)(float('nan'))) self.assertFalse(validators.InRange(0, 10)(None))
def test_raises_if_invalid_arguments(self): with self.assertRaisesRegexp(ValueError, 'Must specify minimum'): validators.InRange() with self.assertRaisesRegexp(ValueError, 'Minimum cannot be greater'): validators.InRange(minimum=10, maximum=0)
class DummyException(Exception): """Raised for testing phases that raise.""" class MyPlug(plugs.BasePlug): """Stub plug for ensuring plugs get mocked correctly.""" def __init__(self): raise NotImplementedError('MyPlug not mocked correctly') def do_stuff(self, unused): raise NotImplementedError('MyPlug not mocked correctly') @plugs.plug(my_plug=MyPlug) @measurements.measures('test_measurement', 'othr_measurement') @measurements.measures('passes', validators=[validators.InRange(1, 10)]) @measurements.measures('fails', validators=[validators.InRange(1, 10)]) @measurements.measures('unset_measurement') def test_phase(phase_data, my_plug): phase_data.logger.error('in phase_data %s', id(phase_data)) phase_data.logger.error('in measurements %s', id(phase_data.measurements)) phase_data.measurements.test_measurement = my_plug.do_stuff('stuff_args') phase_data.measurements.othr_measurement = 0xDEAD phase_data.measurements.passes = 5 phase_data.measurements.fails = 20 phase_data.test_record.AddOutcomeDetails(0xBED) def raising_phase(phase_data): raise DummyException('This Phase raises!')
0, 10).Doc('This measurement is validated.').WithUnits(UOM['SECOND'])) def MeasureSeconds(test): # The 'outcome' of this measurement in the test_record result will be a PASS # because its value passes the validator specified (0 <= 5 <= 10). test.measurements.validated_measurement = 5 # These additional attributes can also be specified inline as kwargs passed # directly to the @measures decorator. If you do so, however, you must # specify exactly one measurement with that decorator (ie. the first argument # must be a string containing the measurement name). If you want to specify # multiple measurements this way, you can stack multiple decorators. @measures('inline_kwargs', docstring='This measurement is declared inline!', units=UOM['HERTZ'], validators=[validators.InRange(0, 10)]) @measures('another_inline', docstring='Because why not?') def InlinePhase(test): # This measurement will have an outcome of FAIL, because the set value of 15 # will not pass the 0 <= x <= 10 validator. test.measurements.inline_kwargs = 15 test.measurements.another_inline = 'This one is unvalidated.' # Let's log a message so the operator knows the test should fail. test.logger.info('Set inline_kwargs to a failing value, test should FAIL!') if __name__ == '__main__': # We instantiate our OpenHTF test with the phases we want to run as args. test = Test(HelloPhase, AgainPhase, LotsOfMeasurements, MeasureSeconds, InlinePhase)