class ConsistentEndDimensionPivotTest(htf_test.TestCase): """Tests validators.ConsistentEndRange. Similar to DimensionPivot.""" _sub_validator = validators.in_range(minimum=5) _test_measurement = htf.Measurement('pivot').with_dimensions( 'time').consistent_end_dimension_pivot_validate(_sub_validator) @htf_test.yields_phases def testPasses(self): @htf.measures(self._test_measurement) def phase(test): test.measurements.pivot[0] = 0 test.measurements.pivot[1] = 2 test.measurements.pivot[2] = 6 test.measurements.pivot[3] = 8 phase_record = yield phase self.assertMeasurementPass(phase_record, 'pivot') @htf_test.yields_phases def testFails(self): @htf.measures(self._test_measurement) def phase(test): test.measurements.pivot[0] = 3 test.measurements.pivot[1] = 4 test.measurements.pivot[2] = 6 test.measurements.pivot[3] = 4 phase_record = yield phase self.assertMeasurementFail(phase_record, 'pivot')
class DimensionPivotTest(htf_test.TestCase): """Tests validators.DimensionPivot. Used with dimensioned measurements.""" _test_value = 10 _sub_validator = validators.in_range(0, _test_value) _test_measurement = htf.Measurement('pivot').with_dimensions( 'height', 'width').dimension_pivot_validate(_sub_validator) @htf_test.yields_phases def testPasses(self): @htf.measures(self._test_measurement) def phase(test): test.measurements.pivot[10, 10] = self._test_value - 2 test.measurements.pivot[11, 10] = self._test_value - 1 phase_record = yield phase self.assertMeasurementPass(phase_record, 'pivot') @htf_test.yields_phases def testFails(self): @htf.measures(self._test_measurement) def phase(test): test.measurements.pivot[11, 12] = self._test_value - 1 test.measurements.pivot[14, 12] = self._test_value + 1 phase_record = yield phase self.assertMeasurementFail(phase_record, 'pivot')
# details, see the validators.py example. @htf.measures(htf.Measurement('validated_measurement').in_range(0, 10).doc( 'This measurement is validated.').with_units(htf.units.SECOND)) def measure_seconds(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. @htf.measures('inline_kwargs', docstring='This measurement is declared inline!', units=htf.units.HERTZ, validators=[validators.in_range(0, 10)]) @htf.measures('another_inline', docstring='Because why not?') def inline_phase(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!') # A multidim measurement including how to convert to a pandas dataframe and # a numpy array. @htf.measures(htf.Measurement('power_time_series') .with_dimensions('ms', 'V', 'A'))
"""Raised for testing phases that raise.""" class MyPlug(base_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.in_range(1, 10)]) @measurements.measures('fails', validators=[validators.in_range(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.add_outcome_details(0xBED) def raising_phase(): raise DummyError('This Phase raises!')
10).doc('This measurement is validated.').with_units(htf.units.SECOND)) def measure_seconds(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. @htf.measures('inline_kwargs', docstring='This measurement is declared inline!', units=htf.units.HERTZ, validators=[validators.in_range(0, 10)]) @htf.measures('another_inline', docstring='Because why not?') def inline_phase(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!') # A multidim measurement including how to convert to a pandas dataframe and # a numpy array. @htf.measures( htf.Measurement('power_time_series').with_dimensions('ms', 'V', 'A'))
test.configure(stop_on_first_failure=True) 2. Using config item. This option lets you toggle this feature dynamically. CONF.load(stop_on_first_failure=True) """ import openhtf as htf from openhtf.output.callbacks import console_summary from openhtf.plugs import user_input from openhtf.util import configuration from openhtf.util import validators CONF = configuration.CONF @htf.measures('number_sum', validators=[validators.in_range(0, 5)]) def add_numbers_fails(test): """Add numbers, but measurement number_sum fails.""" test.logger.info('Add numbers 2 and 4') number_sum = 2 + 4 test.measurements.number_sum = number_sum # This is phase is not expected to run as the add_numbers phase is # expected to fail. @htf.measures(htf.Measurement('hello_world_measurement')) def hello_world(test): """A hello world test phase.""" test.logger.info('This phase will not be run since previous phase failed') test.measurements.hello_world_measurement = 'Hello World!'
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.in_range(1, 10)]) @measurements.measures('fails', validators=[validators.in_range(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.add_outcome_details(0xBED) def raising_phase(phase_data): raise DummyException('This Phase raises!')
plug_away_call_counts: int def __init__(self): super().__init__() self.plug_away_call_counts = 0 def plug_away(self): self.plug_away_call_counts += 1 _DO_STUFF_RETVAL = 0xBEEF @plugs.plug(my_plug=MyPlug, shameless_plug=ShamelessPlug) @openhtf.measures('test_measurement', 'othr_measurement') @openhtf.measures('passes', validators=[validators.in_range(1, 10)]) @openhtf.measures('fails', validators=[validators.in_range(1, 10)]) @openhtf.measures('unset_measurement') def test_phase(phase_data, my_plug, shameless_plug: ShamelessPlug): shameless_plug.plug_away() 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.add_outcome_details(0xBED) @plugs.plug(shameless_plug=ShamelessPlug) def test_phase_with_shameless_plug(phase_data, shameless_plug: ShamelessPlug):