2021-12-03 02:39:18 -05:00
|
|
|
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-11-17 04:09:36 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-12-01 21:14:18 -05:00
|
|
|
# pylint: disable=W0621 # redefined-outer-name
|
2021-11-17 04:09:36 -05:00
|
|
|
|
|
|
|
# This file is a pytest root configuration file and provide the following functionalities:
|
|
|
|
# 1. Defines a few fixtures that could be used under the whole project.
|
|
|
|
# 2. Defines a few hook functions.
|
|
|
|
#
|
|
|
|
# IDF is using [pytest](https://github.com/pytest-dev/pytest) and
|
|
|
|
# [pytest-embedded plugin](https://github.com/espressif/pytest-embedded) as its example test framework.
|
|
|
|
#
|
|
|
|
# This is an experimental feature, and if you found any bug or have any question, please report to
|
|
|
|
# https://github.com/espressif/pytest-embedded/issues
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
2022-01-18 23:12:15 -05:00
|
|
|
import xml.etree.ElementTree as ET
|
2021-11-30 22:35:56 -05:00
|
|
|
from typing import Callable, List, Optional
|
2021-11-17 04:09:36 -05:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from _pytest.config import Config
|
|
|
|
from _pytest.fixtures import FixtureRequest
|
2022-03-07 22:37:42 -05:00
|
|
|
from _pytest.main import Session
|
2021-12-01 21:14:18 -05:00
|
|
|
from _pytest.nodes import Item
|
2022-01-18 23:12:15 -05:00
|
|
|
from _pytest.python import Function
|
2021-11-17 04:09:36 -05:00
|
|
|
from pytest_embedded.plugin import parse_configuration
|
2022-01-18 23:12:15 -05:00
|
|
|
from pytest_embedded.utils import find_by_suffix
|
2021-11-17 04:09:36 -05:00
|
|
|
|
2021-12-03 02:39:18 -05:00
|
|
|
SUPPORTED_TARGETS = ['esp32', 'esp32s2', 'esp32c3', 'esp32s3']
|
2022-01-18 23:12:15 -05:00
|
|
|
PREVIEW_TARGETS = ['linux', 'esp32h2', 'esp32c2']
|
2022-02-18 02:37:39 -05:00
|
|
|
DEFAULT_SDKCONFIG = 'default'
|
2021-12-03 02:39:18 -05:00
|
|
|
|
2021-11-17 04:09:36 -05:00
|
|
|
|
2021-11-30 22:35:56 -05:00
|
|
|
##################
|
|
|
|
# Help Functions #
|
|
|
|
##################
|
|
|
|
def is_target_marker(marker: str) -> bool:
|
2021-11-17 04:09:36 -05:00
|
|
|
if marker.startswith('esp32'):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if marker.startswith('esp8'):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-01-18 23:12:15 -05:00
|
|
|
def format_case_id(target: Optional[str], config: Optional[str], case: str) -> str:
|
2021-11-30 22:35:56 -05:00
|
|
|
return f'{target}.{config}.{case}'
|
|
|
|
|
|
|
|
|
2021-12-03 02:39:18 -05:00
|
|
|
def item_marker_names(item: Item) -> List[str]:
|
|
|
|
return [marker.name for marker in item.iter_markers()]
|
|
|
|
|
|
|
|
|
2021-11-30 22:35:56 -05:00
|
|
|
############
|
|
|
|
# Fixtures #
|
|
|
|
############
|
2021-11-17 04:09:36 -05:00
|
|
|
@pytest.fixture
|
2021-11-30 22:35:56 -05:00
|
|
|
def config(request: FixtureRequest) -> str:
|
2022-02-18 02:37:39 -05:00
|
|
|
return getattr(request, 'param', None) or DEFAULT_SDKCONFIG
|
2021-11-17 04:09:36 -05:00
|
|
|
|
|
|
|
|
2022-02-11 01:58:50 -05:00
|
|
|
@pytest.fixture
|
|
|
|
def test_func_name(request: FixtureRequest) -> str:
|
|
|
|
return request.node.function.__name__ # type: ignore
|
|
|
|
|
|
|
|
|
2022-01-18 23:12:15 -05:00
|
|
|
@pytest.fixture
|
|
|
|
def test_case_name(request: FixtureRequest, target: str, config: str) -> str:
|
|
|
|
return format_case_id(target, config, request.node.originalname)
|
|
|
|
|
|
|
|
|
2021-11-17 04:09:36 -05:00
|
|
|
@pytest.fixture
|
|
|
|
@parse_configuration
|
2022-02-18 02:37:39 -05:00
|
|
|
def build_dir(
|
|
|
|
request: FixtureRequest, app_path: str, target: Optional[str], config: Optional[str]
|
|
|
|
) -> str:
|
2021-11-17 04:09:36 -05:00
|
|
|
"""
|
|
|
|
Check local build dir with the following priority:
|
|
|
|
|
|
|
|
1. build_<target>_<config>
|
|
|
|
2. build_<target>
|
|
|
|
3. build_<config>
|
|
|
|
4. build
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request: pytest fixture
|
|
|
|
app_path: app path
|
|
|
|
target: target
|
|
|
|
config: config
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
valid build directory
|
|
|
|
"""
|
2022-02-18 02:37:39 -05:00
|
|
|
param_or_cli: str = getattr(
|
|
|
|
request, 'param', None
|
2022-03-07 22:37:42 -05:00
|
|
|
) or request.config.getoption('build_dir')
|
2022-02-18 02:37:39 -05:00
|
|
|
if param_or_cli is not None: # respect the param and the cli
|
2021-11-17 04:09:36 -05:00
|
|
|
return param_or_cli
|
|
|
|
|
|
|
|
check_dirs = []
|
|
|
|
if target is not None and config is not None:
|
|
|
|
check_dirs.append(f'build_{target}_{config}')
|
|
|
|
if target is not None:
|
|
|
|
check_dirs.append(f'build_{target}')
|
|
|
|
if config is not None:
|
|
|
|
check_dirs.append(f'build_{config}')
|
|
|
|
check_dirs.append('build')
|
|
|
|
|
|
|
|
for check_dir in check_dirs:
|
|
|
|
binary_path = os.path.join(app_path, check_dir)
|
|
|
|
if os.path.isdir(binary_path):
|
|
|
|
logging.info(f'find valid binary path: {binary_path}')
|
|
|
|
return check_dir
|
|
|
|
|
2022-02-18 02:37:39 -05:00
|
|
|
logging.warning(
|
2022-02-11 01:58:50 -05:00
|
|
|
'checking binary path: %s... missing... try another place', binary_path
|
2022-02-18 02:37:39 -05:00
|
|
|
)
|
2021-11-17 04:09:36 -05:00
|
|
|
|
|
|
|
recommend_place = check_dirs[0]
|
|
|
|
logging.error(
|
2022-02-18 02:37:39 -05:00
|
|
|
f'no build dir valid. Please build the binary via "idf.py -B {recommend_place} build" and run pytest again'
|
|
|
|
)
|
2021-11-17 04:09:36 -05:00
|
|
|
sys.exit(1)
|
2021-11-30 22:35:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2022-02-18 02:37:39 -05:00
|
|
|
def junit_properties(
|
|
|
|
test_case_name: str, record_xml_attribute: Callable[[str, object], None]
|
|
|
|
) -> None:
|
2021-11-30 22:35:56 -05:00
|
|
|
"""
|
|
|
|
This fixture is autoused and will modify the junit report test case name to <target>.<config>.<case_name>
|
|
|
|
"""
|
2022-01-18 23:12:15 -05:00
|
|
|
record_xml_attribute('name', test_case_name)
|
2021-12-01 21:14:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
##################
|
|
|
|
# Hook functions #
|
|
|
|
##################
|
2022-02-18 02:37:39 -05:00
|
|
|
def pytest_addoption(parser: pytest.Parser) -> None:
|
|
|
|
base_group = parser.getgroup('idf')
|
|
|
|
base_group.addoption(
|
|
|
|
'--sdkconfig',
|
|
|
|
help='sdkconfig postfix, like sdkconfig.ci.<config>. (Default: None, which would build all found apps)',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-03-07 22:37:42 -05:00
|
|
|
@pytest.hookimpl(tryfirst=True)
|
|
|
|
def pytest_sessionstart(session: Session) -> None:
|
|
|
|
if session.config.option.target:
|
|
|
|
session.config.option.target = session.config.getoption('target').lower()
|
|
|
|
|
|
|
|
|
2022-01-18 23:12:15 -05:00
|
|
|
@pytest.hookimpl(tryfirst=True)
|
2022-02-18 02:37:39 -05:00
|
|
|
def pytest_collection_modifyitems(config: Config, items: List[Function]) -> None:
|
2022-01-18 23:12:15 -05:00
|
|
|
target = config.getoption('target', None) # use the `build` dir
|
2021-12-01 21:14:18 -05:00
|
|
|
if not target:
|
|
|
|
return
|
|
|
|
|
2022-02-18 02:38:54 -05:00
|
|
|
# sort by file path and callspec.config
|
|
|
|
# implement like this since this is a limitation of pytest, couldn't get fixture values while collecting
|
|
|
|
# https://github.com/pytest-dev/pytest/discussions/9689
|
2022-02-18 02:37:39 -05:00
|
|
|
def _get_param_config(_item: Function) -> str:
|
|
|
|
if hasattr(_item, 'callspec'):
|
|
|
|
return _item.callspec.params.get('config', DEFAULT_SDKCONFIG) # type: ignore
|
|
|
|
return DEFAULT_SDKCONFIG
|
|
|
|
|
2022-02-18 02:38:54 -05:00
|
|
|
items.sort(key=lambda x: (os.path.dirname(x.path), _get_param_config(x)))
|
|
|
|
|
2021-12-03 02:39:18 -05:00
|
|
|
# add markers for special markers
|
|
|
|
for item in items:
|
|
|
|
if 'supported_targets' in item_marker_names(item):
|
2022-01-18 23:12:15 -05:00
|
|
|
for _target in SUPPORTED_TARGETS:
|
|
|
|
item.add_marker(_target)
|
2021-12-03 02:39:18 -05:00
|
|
|
if 'preview_targets' in item_marker_names(item):
|
2022-01-18 23:12:15 -05:00
|
|
|
for _target in PREVIEW_TARGETS:
|
|
|
|
item.add_marker(_target)
|
2021-12-03 02:39:18 -05:00
|
|
|
if 'all_targets' in item_marker_names(item):
|
2022-01-18 23:12:15 -05:00
|
|
|
for _target in [*SUPPORTED_TARGETS, *PREVIEW_TARGETS]:
|
|
|
|
item.add_marker(_target)
|
2021-12-03 02:39:18 -05:00
|
|
|
|
2021-12-01 21:14:18 -05:00
|
|
|
# filter all the test cases with "--target"
|
2021-12-03 02:39:18 -05:00
|
|
|
items[:] = [item for item in items if target in item_marker_names(item)]
|
2022-01-18 23:12:15 -05:00
|
|
|
|
2022-02-18 02:37:39 -05:00
|
|
|
# filter all the test cases with cli option "config"
|
|
|
|
if config.getoption('sdkconfig'):
|
|
|
|
items[:] = [
|
|
|
|
item
|
|
|
|
for item in items
|
|
|
|
if _get_param_config(item) == config.getoption('sdkconfig')
|
|
|
|
]
|
|
|
|
|
2022-01-18 23:12:15 -05:00
|
|
|
|
|
|
|
@pytest.hookimpl(trylast=True)
|
|
|
|
def pytest_runtest_teardown(item: Function) -> None:
|
|
|
|
"""
|
|
|
|
Format the test case generated junit reports
|
|
|
|
"""
|
|
|
|
tempdir = item.funcargs.get('test_case_tempdir')
|
|
|
|
if not tempdir:
|
|
|
|
return
|
|
|
|
|
|
|
|
junits = find_by_suffix('.xml', tempdir)
|
|
|
|
if not junits:
|
|
|
|
return
|
|
|
|
|
|
|
|
target = item.funcargs['target']
|
|
|
|
config = item.funcargs['config']
|
|
|
|
for junit in junits:
|
|
|
|
xml = ET.parse(junit)
|
|
|
|
testcases = xml.findall('.//testcase')
|
|
|
|
for case in testcases:
|
|
|
|
case.attrib['name'] = format_case_id(target, config, case.attrib['name'])
|
|
|
|
if 'file' in case.attrib:
|
2022-02-18 02:37:39 -05:00
|
|
|
case.attrib['file'] = case.attrib['file'].replace(
|
|
|
|
'/IDF/', ''
|
|
|
|
) # our unity test framework
|
2022-01-18 23:12:15 -05:00
|
|
|
xml.write(junit)
|