TestCase.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import yaml
  15. class TestCase(object):
  16. """
  17. Test Case Object, mainly used with runner.
  18. runner can parse all test cases from a given path, set data and config for test case in prepare stage.
  19. TestCase instance will record these data, provide run method to let runner execute test case.
  20. :param test_method: test function
  21. :param extra_data: data passed to test function
  22. :param overwrite_args: kwargs that overwrite original test case configs
  23. """
  24. DEFAULT_CASE_DOC = dict()
  25. def __init__(self, test_method, extra_data, **overwrite_args):
  26. self.test_method = test_method
  27. self.extra_data = extra_data
  28. self.overwrite_args = overwrite_args
  29. def run(self):
  30. """ execute the test case """
  31. return self.test_method(self.extra_data, **self.overwrite_args)
  32. def document(self):
  33. """
  34. generate test case document.
  35. parse the case doc with yaml parser and update to original case attributes.
  36. :return: case document, dict of case attributes and values
  37. """
  38. doc_string = self.test_method.__doc__
  39. try:
  40. doc = yaml.load(doc_string, Loader=yaml.Loader)
  41. except (AttributeError, OSError, UnicodeDecodeError):
  42. doc = self.DEFAULT_CASE_DOC
  43. doc.update(self.test_method.env_args)
  44. doc.update(self.test_method.accepted_filter)
  45. return doc