conftest.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. # pylint: disable=W0621 # redefined-outer-name
  4. # This file is a pytest root configuration file and provide the following functionalities:
  5. # 1. Defines a few fixtures that could be used under the whole project.
  6. # 2. Defines a few hook functions.
  7. #
  8. # IDF is using [pytest](https://github.com/pytest-dev/pytest) and
  9. # [pytest-embedded plugin](https://github.com/espressif/pytest-embedded) as its example test framework.
  10. #
  11. # This is an experimental feature, and if you found any bug or have any question, please report to
  12. # https://github.com/espressif/pytest-embedded/issues
  13. import logging
  14. import os
  15. import sys
  16. import xml.etree.ElementTree as ET
  17. from typing import Callable, List, Optional
  18. import pytest
  19. from _pytest.config import Config
  20. from _pytest.fixtures import FixtureRequest
  21. from _pytest.main import Session
  22. from _pytest.nodes import Item
  23. from _pytest.python import Function
  24. from pytest_embedded.plugin import parse_configuration
  25. from pytest_embedded.utils import find_by_suffix
  26. SUPPORTED_TARGETS = ['esp32', 'esp32s2', 'esp32c3', 'esp32s3']
  27. PREVIEW_TARGETS = ['linux', 'esp32h2', 'esp32c2']
  28. DEFAULT_SDKCONFIG = 'default'
  29. ##################
  30. # Help Functions #
  31. ##################
  32. def is_target_marker(marker: str) -> bool:
  33. if marker.startswith('esp32'):
  34. return True
  35. if marker.startswith('esp8'):
  36. return True
  37. return False
  38. def format_case_id(target: Optional[str], config: Optional[str], case: str) -> str:
  39. return f'{target}.{config}.{case}'
  40. def item_marker_names(item: Item) -> List[str]:
  41. return [marker.name for marker in item.iter_markers()]
  42. ############
  43. # Fixtures #
  44. ############
  45. @pytest.fixture
  46. def config(request: FixtureRequest) -> str:
  47. return getattr(request, 'param', None) or DEFAULT_SDKCONFIG
  48. @pytest.fixture
  49. def test_func_name(request: FixtureRequest) -> str:
  50. return request.node.function.__name__ # type: ignore
  51. @pytest.fixture
  52. def test_case_name(request: FixtureRequest, target: str, config: str) -> str:
  53. return format_case_id(target, config, request.node.originalname)
  54. @pytest.fixture
  55. @parse_configuration
  56. def build_dir(
  57. request: FixtureRequest, app_path: str, target: Optional[str], config: Optional[str]
  58. ) -> str:
  59. """
  60. Check local build dir with the following priority:
  61. 1. build_<target>_<config>
  62. 2. build_<target>
  63. 3. build_<config>
  64. 4. build
  65. Args:
  66. request: pytest fixture
  67. app_path: app path
  68. target: target
  69. config: config
  70. Returns:
  71. valid build directory
  72. """
  73. param_or_cli: str = getattr(
  74. request, 'param', None
  75. ) or request.config.getoption('build_dir')
  76. if param_or_cli is not None: # respect the param and the cli
  77. return param_or_cli
  78. check_dirs = []
  79. if target is not None and config is not None:
  80. check_dirs.append(f'build_{target}_{config}')
  81. if target is not None:
  82. check_dirs.append(f'build_{target}')
  83. if config is not None:
  84. check_dirs.append(f'build_{config}')
  85. check_dirs.append('build')
  86. for check_dir in check_dirs:
  87. binary_path = os.path.join(app_path, check_dir)
  88. if os.path.isdir(binary_path):
  89. logging.info(f'find valid binary path: {binary_path}')
  90. return check_dir
  91. logging.warning(
  92. 'checking binary path: %s... missing... try another place', binary_path
  93. )
  94. recommend_place = check_dirs[0]
  95. logging.error(
  96. f'no build dir valid. Please build the binary via "idf.py -B {recommend_place} build" and run pytest again'
  97. )
  98. sys.exit(1)
  99. @pytest.fixture(autouse=True)
  100. def junit_properties(
  101. test_case_name: str, record_xml_attribute: Callable[[str, object], None]
  102. ) -> None:
  103. """
  104. This fixture is autoused and will modify the junit report test case name to <target>.<config>.<case_name>
  105. """
  106. record_xml_attribute('name', test_case_name)
  107. ##################
  108. # Hook functions #
  109. ##################
  110. def pytest_addoption(parser: pytest.Parser) -> None:
  111. base_group = parser.getgroup('idf')
  112. base_group.addoption(
  113. '--sdkconfig',
  114. help='sdkconfig postfix, like sdkconfig.ci.<config>. (Default: None, which would build all found apps)',
  115. )
  116. @pytest.hookimpl(tryfirst=True)
  117. def pytest_sessionstart(session: Session) -> None:
  118. if session.config.option.target:
  119. session.config.option.target = session.config.getoption('target').lower()
  120. @pytest.hookimpl(tryfirst=True)
  121. def pytest_collection_modifyitems(config: Config, items: List[Function]) -> None:
  122. target = config.getoption('target', None) # use the `build` dir
  123. if not target:
  124. return
  125. # sort by file path and callspec.config
  126. # implement like this since this is a limitation of pytest, couldn't get fixture values while collecting
  127. # https://github.com/pytest-dev/pytest/discussions/9689
  128. def _get_param_config(_item: Function) -> str:
  129. if hasattr(_item, 'callspec'):
  130. return _item.callspec.params.get('config', DEFAULT_SDKCONFIG) # type: ignore
  131. return DEFAULT_SDKCONFIG
  132. items.sort(key=lambda x: (os.path.dirname(x.path), _get_param_config(x)))
  133. # add markers for special markers
  134. for item in items:
  135. if 'supported_targets' in item_marker_names(item):
  136. for _target in SUPPORTED_TARGETS:
  137. item.add_marker(_target)
  138. if 'preview_targets' in item_marker_names(item):
  139. for _target in PREVIEW_TARGETS:
  140. item.add_marker(_target)
  141. if 'all_targets' in item_marker_names(item):
  142. for _target in [*SUPPORTED_TARGETS, *PREVIEW_TARGETS]:
  143. item.add_marker(_target)
  144. # filter all the test cases with "--target"
  145. items[:] = [item for item in items if target in item_marker_names(item)]
  146. # filter all the test cases with cli option "config"
  147. if config.getoption('sdkconfig'):
  148. items[:] = [
  149. item
  150. for item in items
  151. if _get_param_config(item) == config.getoption('sdkconfig')
  152. ]
  153. @pytest.hookimpl(trylast=True)
  154. def pytest_runtest_teardown(item: Function) -> None:
  155. """
  156. Format the test case generated junit reports
  157. """
  158. tempdir = item.funcargs.get('test_case_tempdir')
  159. if not tempdir:
  160. return
  161. junits = find_by_suffix('.xml', tempdir)
  162. if not junits:
  163. return
  164. target = item.funcargs['target']
  165. config = item.funcargs['config']
  166. for junit in junits:
  167. xml = ET.parse(junit)
  168. testcases = xml.findall('.//testcase')
  169. for case in testcases:
  170. case.attrib['name'] = format_case_id(target, config, case.attrib['name'])
  171. if 'file' in case.attrib:
  172. case.attrib['file'] = case.attrib['file'].replace(
  173. '/IDF/', ''
  174. ) # our unity test framework
  175. xml.write(junit)