conftest.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # SPDX-FileCopyrightText: 2021 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. from typing import Callable, List, Optional
  17. import pytest
  18. from _pytest.config import Config
  19. from _pytest.fixtures import FixtureRequest
  20. from _pytest.nodes import Item
  21. from pytest_embedded.plugin import parse_configuration
  22. from pytest_embedded_idf.app import IdfApp
  23. ##################
  24. # Help Functions #
  25. ##################
  26. def is_target_marker(marker: str) -> bool:
  27. if marker.startswith('esp32'):
  28. return True
  29. if marker.startswith('esp8'):
  30. return True
  31. return False
  32. def format_case_id(target: str, config: str, case: str) -> str:
  33. return f'{target}.{config}.{case}'
  34. ############
  35. # Fixtures #
  36. ############
  37. @pytest.fixture(scope='session')
  38. def target_markers(pytestconfig: Config) -> List[str]:
  39. res = []
  40. for item in pytestconfig.getini('markers'):
  41. marker = item.split(':')[0]
  42. if is_target_marker(marker):
  43. res.append(marker)
  44. return res
  45. @pytest.fixture(scope='session')
  46. def env_markers(pytestconfig: Config) -> List[str]:
  47. res = []
  48. for item in pytestconfig.getini('markers'):
  49. marker = item.split(':')[0]
  50. if not marker.startswith('esp32'):
  51. res.append(marker)
  52. return res
  53. @pytest.fixture
  54. def config(request: FixtureRequest) -> str:
  55. return getattr(request, 'param', None) or request.config.getoption('config', 'default') # type: ignore
  56. @pytest.fixture
  57. @parse_configuration
  58. def build_dir(request: FixtureRequest, app_path: str, target: Optional[str], config: Optional[str]) -> 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(request, 'param', None) or request.config.option.__dict__.get('build_dir')
  74. if param_or_cli is not None: # respect the parametrize and the cli
  75. return param_or_cli
  76. check_dirs = []
  77. if target is not None and config is not None:
  78. check_dirs.append(f'build_{target}_{config}')
  79. if target is not None:
  80. check_dirs.append(f'build_{target}')
  81. if config is not None:
  82. check_dirs.append(f'build_{config}')
  83. check_dirs.append('build')
  84. for check_dir in check_dirs:
  85. binary_path = os.path.join(app_path, check_dir)
  86. if os.path.isdir(binary_path):
  87. logging.info(f'find valid binary path: {binary_path}')
  88. return check_dir
  89. logging.warning(f'checking binary path: {binary_path}... missing... try another place')
  90. recommend_place = check_dirs[0]
  91. logging.error(
  92. f'no build dir valid. Please build the binary via "idf.py -B {recommend_place} build" and run pytest again')
  93. sys.exit(1)
  94. @pytest.fixture(autouse=True)
  95. def junit_properties(app: IdfApp, config: str, test_case_name: str,
  96. record_xml_attribute: Callable[[str, object], None]) -> None:
  97. """
  98. This fixture is autoused and will modify the junit report test case name to <target>.<config>.<case_name>
  99. """
  100. record_xml_attribute('name', format_case_id(app.target, config, test_case_name))
  101. ##################
  102. # Hook functions #
  103. ##################
  104. @pytest.hookimpl(trylast=True)
  105. def pytest_collection_modifyitems(config: Config, items: List[Item]) -> None:
  106. target = config.getoption('target', None)
  107. if not target:
  108. return
  109. # filter all the test cases with "--target"
  110. items[:] = [item for item in items if target in [marker.name for marker in item.iter_markers()]]