TestCase.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. try:
  16. from yaml import CLoader as Loader
  17. except ImportError:
  18. from yaml import Loader as Loader
  19. class TestCase(object):
  20. """
  21. Test Case Object, mainly used with runner.
  22. runner can parse all test cases from a given path, set data and config for test case in prepare stage.
  23. TestCase instance will record these data, provide run method to let runner execute test case.
  24. :param test_method: test function
  25. :param extra_data: data passed to test function
  26. :param overwrite_args: kwargs that overwrite original test case configs
  27. """
  28. DEFAULT_CASE_DOC = dict()
  29. def __init__(self, test_method, extra_data, **overwrite_args):
  30. self.test_method = test_method
  31. self.extra_data = extra_data
  32. self.overwrite_args = overwrite_args
  33. def run(self):
  34. """ execute the test case """
  35. return self.test_method(self.extra_data, **self.overwrite_args)
  36. def document(self):
  37. """
  38. generate test case document.
  39. parse the case doc with yaml parser and update to original case attributes.
  40. :return: case document, dict of case attributes and values
  41. """
  42. doc_string = self.test_method.__doc__
  43. try:
  44. doc = yaml.load(doc_string, Loader=Loader)
  45. except (AttributeError, OSError, UnicodeDecodeError):
  46. doc = self.DEFAULT_CASE_DOC
  47. doc.update(self.test_method.env_args)
  48. doc.update(self.test_method.accepted_filter)
  49. return doc