Beispiel #1
0
    def _handle_test(self, test):
        self._initialize_plugs(test.descriptor.plug_types)

        # We'll need a place to stash the resulting TestRecord.
        record_saver = util.NonLocalResult()
        test.add_output_callbacks(
            lambda record: setattr(record_saver, 'result', record))

        profile_filepath = self.test_case.get_profile_filepath()
        if profile_filepath is None:
            profile_tempfile = None
        else:
            profile_tempfile = tempfile.NamedTemporaryFile()
        # Mock the PlugManager to use ours instead, and execute the test.
        with mock.patch.object(plugs,
                               'PlugManager',
                               new=lambda _, __: self.plug_manager):
            test.execute(test_start=self.test_case.test_start_function,
                         profile_filename=(None if profile_tempfile is None
                                           else profile_tempfile.name))

        if profile_tempfile is not None:
            _merge_stats(pstats.Stats(profile_tempfile.name), profile_filepath)
            profile_tempfile.close()

        test_record_ = record_saver.result
        if test_record_.outcome_details:
            msgs = []
            for detail in test_record_.outcome_details:
                msgs.append('code: {}\ndescription: {}'.format(
                    detail.code, detail.description))
            failure_message = '\n'.join(msgs)
        else:
            failure_message = None
        return test_record_, failure_message
Beispiel #2
0
  def _handle_test(self, test):
    self._initialize_plugs(test.descriptor.plug_types)
    # Make sure we inject our mock plug instances.
    for plug_type, plug_value in six.iteritems(self.mock_plugs):
      self.plug_manager.update_plug(plug_type, plug_value)

    # We'll need a place to stash the resulting TestRecord.
    record_saver = util.NonLocalResult()
    test.add_output_callbacks(
        lambda record: setattr(record_saver, 'result', record))

    # Mock the PlugManager to use ours instead, and execute the test.
    with mock.patch(
        'openhtf.plugs.PlugManager', new=lambda _, __: self.plug_manager):
      test.execute(test_start=self.test_case.test_start_function)

    test_record_ = record_saver.result
    if test_record_.outcome_details:
      msgs = []
      for detail in test_record_.outcome_details:
        msgs.append('code: {}\ndescription: {}'.format(detail.code,
                                                       detail.description))
      failure_message = '\n'.join(msgs)
    else:
      failure_message = None
    return test_record_, failure_message
Beispiel #3
0
 def setUpClass(cls):
   # Create input record.
   result = util.NonLocalResult()
   def _save_result(test_record):
     result.result = test_record
   cls._test = htf.Test(
       all_the_things.hello_world,
       all_the_things.dimensions,
       all_the_things.attachments,
   )
   cls._test.add_output_callbacks(_save_result)
   cls._test.make_uid = lambda: 'UNITTEST:MOCK:UID'
Beispiel #4
0
    def _handle_test(self, test):
        self._initialize_plugs(test.descriptor.plug_types)
        # Make sure we inject our mock plug instances.
        for plug_type, plug_value in six.iteritems(self.mock_plugs):
            self.plug_manager.update_plug(plug_type, plug_value)

        # We'll need a place to stash the resulting TestRecord.
        record_saver = util.NonLocalResult()
        test.add_output_callbacks(
            lambda record: setattr(record_saver, 'result', record))

        # Mock the PlugManager to use ours instead, and execute the test.
        with mock.patch('openhtf.plugs.PlugManager',
                        new=lambda _, __: self.plug_manager):
            test.execute(test_start=lambda: 'TestDutId')

        return record_saver.result
Beispiel #5
0
 def setUpClass(cls):
   # Create input record.
   result = util.NonLocalResult()
   def _save_result(test_record):
     result.result = test_record
   cls._test = htf.Test(
       all_the_things.hello_world,
       all_the_things.dimensions,
       all_the_things.attachments,
       # We intentionally call dimensions and attachments phases twice so we
       # can check functionality for non-unique measurement and attachment
       # names.
       all_the_things.hello_world,
       all_the_things.attachments,
   )
   cls._test.add_output_callbacks(_save_result)
   cls._test.make_uid = lambda: 'UNITTEST:MOCK:UID'
Beispiel #6
0
    def test_measurements(self):
        result = util.NonLocalResult()

        def _save_result(test_record):
            result.result = test_record

        test = htf.Test(hello_phase, again_phase, lots_of_measurements,
                        measure_seconds, measure_dimensions, inline_phase)

        if self.UPDATE_OUTPUT:
            test.add_output_callbacks(callbacks.OutputToFile(RECORD_FILENAME))
        else:
            test.add_output_callbacks(_save_result)
        test.make_uid = lambda: 'UNITTEST:MOCK:UID'
        test.execute(test_start=lambda: 'TestDUT')
        if not self.UPDATE_OUTPUT:
            data.assert_records_equal_nonvolatile(self.record, result.result,
                                                  _VOLATILE_FIELDS)
Beispiel #7
0
    def testMeasurements(self):
        result = util.NonLocalResult()

        def _SaveResult(test_record):
            result.result = test_record

        test = Test(HelloPhase, AgainPhase, LotsOfMeasurements, MeasureSeconds,
                    InlinePhase)
        # No need to run the http_api, we just want to generate the test record.
        test.Configure(http_port=None)
        if self.UPDATE_OUTPUT:
            test.AddOutputCallbacks(_PickleRecord)
        test.AddOutputCallbacks(_SaveResult)
        test.Execute(test_start=lambda: 'TestDUT')
        if self.UPDATE_OUTPUT:
            with open(_LocalFilename('measurements_record.pickle'),
                      'wb') as pfile:
                pickle.dump(result.result, pfile, -1)
        else:
            data.AssertRecordsEqualNonvolatile(self.record, result.result,
                                               _VOLATILE_FIELDS)