2023-07-06 15:58:59 +00:00
|
|
|
# SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
|
2021-11-17 09:09:36 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-12-02 02:14:18 +00:00
|
|
|
# pylint: disable=W0621 # redefined-outer-name
|
2021-11-17 09:09:36 +00: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
|
2022-10-21 03:46:24 +00:00
|
|
|
|
2023-05-24 02:53:57 +00:00
|
|
|
import glob
|
|
|
|
import json
|
2021-11-17 09:09:36 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2022-12-15 03:41:09 +00:00
|
|
|
import re
|
2021-11-17 09:09:36 +00:00
|
|
|
import sys
|
2023-09-18 08:59:05 +00:00
|
|
|
from copy import deepcopy
|
2022-04-02 08:47:58 +00:00
|
|
|
from datetime import datetime
|
2023-07-31 04:49:08 +00:00
|
|
|
from typing import Callable, Optional
|
2021-11-17 09:09:36 +00:00
|
|
|
|
|
|
|
import pytest
|
2023-07-31 04:49:08 +00:00
|
|
|
from _pytest.config import Config
|
2021-11-17 09:09:36 +00:00
|
|
|
from _pytest.fixtures import FixtureRequest
|
2022-04-25 09:26:29 +00:00
|
|
|
from pytest_embedded.plugin import multi_dut_argument, multi_dut_fixture
|
2022-05-18 06:59:34 +00:00
|
|
|
from pytest_embedded_idf.dut import IdfDut
|
2021-11-17 09:09:36 +00:00
|
|
|
|
2022-09-22 13:46:56 +00:00
|
|
|
try:
|
2023-07-31 04:49:08 +00:00
|
|
|
from idf_ci_utils import IDF_PATH
|
|
|
|
from idf_pytest.constants import DEFAULT_SDKCONFIG, ENV_MARKERS, SPECIAL_MARKERS, TARGET_MARKERS
|
|
|
|
from idf_pytest.plugin import IDF_PYTEST_EMBEDDED_KEY, IdfPytestEmbedded
|
|
|
|
from idf_pytest.utils import format_case_id, get_target_marker_from_expr
|
2022-11-09 17:58:45 +00:00
|
|
|
from idf_unity_tester import CaseTester
|
2022-09-22 13:46:56 +00:00
|
|
|
except ImportError:
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), 'tools', 'ci'))
|
2023-07-31 04:49:08 +00:00
|
|
|
from idf_ci_utils import IDF_PATH
|
|
|
|
from idf_pytest.constants import DEFAULT_SDKCONFIG, ENV_MARKERS, SPECIAL_MARKERS, TARGET_MARKERS
|
|
|
|
from idf_pytest.plugin import IDF_PYTEST_EMBEDDED_KEY, IdfPytestEmbedded
|
|
|
|
from idf_pytest.utils import format_case_id, get_target_marker_from_expr
|
2022-09-22 13:46:56 +00:00
|
|
|
from idf_unity_tester import CaseTester
|
|
|
|
|
2022-07-14 05:17:39 +00:00
|
|
|
try:
|
|
|
|
import common_test_methods # noqa: F401
|
|
|
|
except ImportError:
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), 'tools', 'ci', 'python_packages'))
|
|
|
|
import common_test_methods # noqa: F401
|
|
|
|
|
2023-07-06 15:58:59 +00:00
|
|
|
|
2021-12-01 03:35:56 +00:00
|
|
|
############
|
|
|
|
# Fixtures #
|
|
|
|
############
|
2022-12-01 09:40:03 +00:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def idf_path() -> str:
|
|
|
|
return os.path.dirname(__file__)
|
|
|
|
|
|
|
|
|
2022-04-02 08:47:58 +00:00
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def session_tempdir() -> str:
|
2022-07-25 02:52:54 +00:00
|
|
|
_tmpdir = os.path.join(
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
'pytest_embedded_log',
|
|
|
|
datetime.now().strftime('%Y-%m-%d_%H-%M-%S'),
|
|
|
|
)
|
|
|
|
os.makedirs(_tmpdir, exist_ok=True)
|
|
|
|
return _tmpdir
|
2022-04-02 08:47:58 +00:00
|
|
|
|
|
|
|
|
2022-09-22 13:46:56 +00:00
|
|
|
@pytest.fixture
|
2022-11-22 03:13:14 +00:00
|
|
|
def case_tester(dut: IdfDut, **kwargs): # type: ignore
|
2022-09-22 13:46:56 +00:00
|
|
|
yield CaseTester(dut, **kwargs)
|
|
|
|
|
|
|
|
|
2021-11-17 09:09:36 +00:00
|
|
|
@pytest.fixture
|
2022-04-25 09:26:29 +00:00
|
|
|
@multi_dut_argument
|
2021-12-01 03:35:56 +00:00
|
|
|
def config(request: FixtureRequest) -> str:
|
2023-07-31 04:49:08 +00:00
|
|
|
return getattr(request, 'param', None) or DEFAULT_SDKCONFIG # type: ignore
|
2021-11-17 09:09:36 +00:00
|
|
|
|
|
|
|
|
2022-02-11 06:58:50 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def test_func_name(request: FixtureRequest) -> str:
|
|
|
|
return request.node.function.__name__ # type: ignore
|
|
|
|
|
|
|
|
|
2022-01-19 04:12:15 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def test_case_name(request: FixtureRequest, target: str, config: str) -> str:
|
2023-07-25 02:06:57 +00:00
|
|
|
is_qemu = request._pyfuncitem.get_closest_marker('qemu') is not None
|
2023-09-18 08:59:05 +00:00
|
|
|
if hasattr(request._pyfuncitem, 'callspec'):
|
|
|
|
params = deepcopy(request._pyfuncitem.callspec.params) # type: ignore
|
|
|
|
else:
|
|
|
|
params = {}
|
|
|
|
|
|
|
|
filtered_params = {}
|
|
|
|
for k, v in params.items():
|
|
|
|
if k not in request.session._fixturemanager._arg2fixturedefs: # type: ignore
|
|
|
|
filtered_params[k] = v # not fixture ones
|
|
|
|
|
|
|
|
return format_case_id(target, config, request.node.originalname, is_qemu=is_qemu, params=filtered_params) # type: ignore
|
2022-01-19 04:12:15 +00:00
|
|
|
|
|
|
|
|
2021-11-17 09:09:36 +00:00
|
|
|
@pytest.fixture
|
2022-04-25 09:26:29 +00:00
|
|
|
@multi_dut_fixture
|
2023-06-20 03:10:47 +00:00
|
|
|
def build_dir(app_path: str, target: Optional[str], config: Optional[str]) -> str:
|
2021-11-17 09:09:36 +00:00
|
|
|
"""
|
|
|
|
Check local build dir with the following priority:
|
|
|
|
|
|
|
|
1. build_<target>_<config>
|
|
|
|
2. build_<target>
|
|
|
|
3. build_<config>
|
|
|
|
4. build
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
valid build directory
|
|
|
|
"""
|
2023-03-21 03:39:56 +00:00
|
|
|
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')
|
2021-11-17 09:09:36 +00:00
|
|
|
|
|
|
|
for check_dir in check_dirs:
|
|
|
|
binary_path = os.path.join(app_path, check_dir)
|
|
|
|
if os.path.isdir(binary_path):
|
2023-06-20 03:10:47 +00:00
|
|
|
logging.info(f'found valid binary path: {binary_path}')
|
2021-11-17 09:09:36 +00:00
|
|
|
return check_dir
|
|
|
|
|
2022-12-01 02:18:44 +00:00
|
|
|
logging.warning('checking binary path: %s... missing... try another place', binary_path)
|
2021-11-17 09:09:36 +00:00
|
|
|
|
2022-10-21 03:46:24 +00:00
|
|
|
raise ValueError(
|
2023-05-24 02:53:57 +00:00
|
|
|
f'no build dir valid. Please build the binary via "idf.py -B {check_dirs[0]} build" and run pytest again'
|
2022-02-18 07:37:39 +00:00
|
|
|
)
|
2021-12-01 03:35:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2022-04-25 09:26:29 +00:00
|
|
|
@multi_dut_fixture
|
2022-12-01 02:18:44 +00:00
|
|
|
def junit_properties(test_case_name: str, record_xml_attribute: Callable[[str, object], None]) -> None:
|
2021-12-01 03:35:56 +00:00
|
|
|
"""
|
|
|
|
This fixture is autoused and will modify the junit report test case name to <target>.<config>.<case_name>
|
|
|
|
"""
|
2022-01-19 04:12:15 +00:00
|
|
|
record_xml_attribute('name', test_case_name)
|
2021-12-02 02:14:18 +00:00
|
|
|
|
|
|
|
|
2023-10-23 10:59:25 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def set_test_case_name(request: FixtureRequest, test_case_name: str) -> None:
|
|
|
|
request.node.funcargs['test_case_name'] = test_case_name
|
|
|
|
|
|
|
|
|
2022-12-15 03:41:09 +00:00
|
|
|
######################
|
|
|
|
# Log Util Functions #
|
|
|
|
######################
|
|
|
|
@pytest.fixture
|
|
|
|
def log_performance(record_property: Callable[[str, object], None]) -> Callable[[str, str], None]:
|
|
|
|
"""
|
|
|
|
log performance item with pre-defined format to the console
|
|
|
|
and record it under the ``properties`` tag in the junit report if available.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def real_func(item: str, value: str) -> None:
|
|
|
|
"""
|
|
|
|
:param item: performance item name
|
|
|
|
:param value: performance value
|
|
|
|
"""
|
|
|
|
logging.info('[Performance][%s]: %s', item, value)
|
|
|
|
record_property(item, value)
|
|
|
|
|
|
|
|
return real_func
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def check_performance(idf_path: str) -> Callable[[str, float, str], None]:
|
|
|
|
"""
|
|
|
|
check if the given performance item meets the passing standard or not
|
|
|
|
"""
|
|
|
|
|
|
|
|
def real_func(item: str, value: float, target: str) -> None:
|
|
|
|
"""
|
|
|
|
:param item: performance item name
|
|
|
|
:param value: performance item value
|
|
|
|
:param target: target chip
|
|
|
|
:raise: AssertionError: if check fails
|
|
|
|
"""
|
2023-01-10 06:58:02 +00:00
|
|
|
|
2022-12-15 03:41:09 +00:00
|
|
|
def _find_perf_item(operator: str, path: str) -> float:
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
data = f.read()
|
|
|
|
match = re.search(r'#define\s+IDF_PERFORMANCE_{}_{}\s+([\d.]+)'.format(operator, item.upper()), data)
|
|
|
|
return float(match.group(1)) # type: ignore
|
|
|
|
|
|
|
|
def _check_perf(operator: str, standard_value: float) -> None:
|
|
|
|
if operator == 'MAX':
|
|
|
|
ret = value <= standard_value
|
|
|
|
else:
|
|
|
|
ret = value >= standard_value
|
|
|
|
if not ret:
|
|
|
|
raise AssertionError(
|
|
|
|
"[Performance] {} value is {}, doesn't meet pass standard {}".format(item, value, standard_value)
|
|
|
|
)
|
|
|
|
|
|
|
|
path_prefix = os.path.join(idf_path, 'components', 'idf_test', 'include')
|
|
|
|
performance_files = (
|
|
|
|
os.path.join(path_prefix, target, 'idf_performance_target.h'),
|
|
|
|
os.path.join(path_prefix, 'idf_performance.h'),
|
|
|
|
)
|
|
|
|
|
|
|
|
found_item = False
|
|
|
|
for op in ['MIN', 'MAX']:
|
|
|
|
for performance_file in performance_files:
|
|
|
|
try:
|
|
|
|
standard = _find_perf_item(op, performance_file)
|
|
|
|
except (IOError, AttributeError):
|
|
|
|
# performance file doesn't exist or match is not found in it
|
|
|
|
continue
|
|
|
|
|
|
|
|
_check_perf(op, standard)
|
|
|
|
found_item = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not found_item:
|
|
|
|
raise AssertionError('Failed to get performance standard for {}'.format(item))
|
|
|
|
|
|
|
|
return real_func
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def log_minimum_free_heap_size(dut: IdfDut, config: str) -> Callable[..., None]:
|
|
|
|
def real_func() -> None:
|
|
|
|
res = dut.expect(r'Minimum free heap size: (\d+) bytes')
|
|
|
|
logging.info(
|
|
|
|
'\n------ heap size info ------\n'
|
|
|
|
'[app_name] {}\n'
|
|
|
|
'[config_name] {}\n'
|
|
|
|
'[target] {}\n'
|
|
|
|
'[minimum_free_heap_size] {} Bytes\n'
|
|
|
|
'------ heap size end ------'.format(
|
|
|
|
os.path.basename(dut.app.app_path),
|
|
|
|
config,
|
|
|
|
dut.target,
|
|
|
|
res.group(1).decode('utf8'),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return real_func
|
|
|
|
|
|
|
|
|
2023-03-31 15:24:16 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def dev_password(request: FixtureRequest) -> str:
|
|
|
|
return request.config.getoption('dev_passwd') or ''
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def dev_user(request: FixtureRequest) -> str:
|
|
|
|
return request.config.getoption('dev_user') or ''
|
|
|
|
|
|
|
|
|
2021-12-02 02:14:18 +00:00
|
|
|
##################
|
|
|
|
# Hook functions #
|
|
|
|
##################
|
2022-02-18 07:37:39 +00:00
|
|
|
def pytest_addoption(parser: pytest.Parser) -> None:
|
2023-05-24 02:53:57 +00:00
|
|
|
idf_group = parser.getgroup('idf')
|
|
|
|
idf_group.addoption(
|
2022-02-18 07:37:39 +00:00
|
|
|
'--sdkconfig',
|
|
|
|
help='sdkconfig postfix, like sdkconfig.ci.<config>. (Default: None, which would build all found apps)',
|
|
|
|
)
|
2023-05-24 02:53:57 +00:00
|
|
|
idf_group.addoption(
|
2023-03-31 15:24:16 +00:00
|
|
|
'--dev-user',
|
|
|
|
help='user name associated with some specific device/service used during the test execution',
|
|
|
|
)
|
2023-05-24 02:53:57 +00:00
|
|
|
idf_group.addoption(
|
2023-03-31 15:24:16 +00:00
|
|
|
'--dev-passwd',
|
|
|
|
help='password associated with some specific device/service used during the test execution',
|
|
|
|
)
|
2023-05-24 02:53:57 +00:00
|
|
|
idf_group.addoption(
|
|
|
|
'--app-info-basedir',
|
|
|
|
default=IDF_PATH,
|
|
|
|
help='app info base directory. specify this value when you\'re building under a '
|
2023-07-06 15:58:59 +00:00
|
|
|
'different IDF_PATH. (Default: $IDF_PATH)',
|
2023-05-24 02:53:57 +00:00
|
|
|
)
|
|
|
|
idf_group.addoption(
|
|
|
|
'--app-info-filepattern',
|
|
|
|
help='glob pattern to specify the files that include built app info generated by '
|
2023-07-06 15:58:59 +00:00
|
|
|
'`idf-build-apps --collect-app-info ...`. will not raise ValueError when binary '
|
|
|
|
'paths not exist in local file system if not listed recorded in the app info.',
|
2023-05-24 02:53:57 +00:00
|
|
|
)
|
2022-02-18 07:37:39 +00:00
|
|
|
|
|
|
|
|
2022-03-14 03:53:36 +00:00
|
|
|
def pytest_configure(config: Config) -> None:
|
2022-05-21 16:38:17 +00:00
|
|
|
# cli option "--target"
|
|
|
|
target = config.getoption('target') or ''
|
|
|
|
|
|
|
|
help_commands = ['--help', '--fixtures', '--markers', '--version']
|
|
|
|
for cmd in help_commands:
|
|
|
|
if cmd in config.invocation_params.args:
|
|
|
|
target = 'unneeded'
|
|
|
|
break
|
|
|
|
|
|
|
|
if not target: # also could specify through markexpr via "-m"
|
2022-11-29 09:48:36 +00:00
|
|
|
target = get_target_marker_from_expr(config.getoption('markexpr') or '')
|
2022-05-21 16:38:17 +00:00
|
|
|
|
2023-05-24 02:53:57 +00:00
|
|
|
apps_list = None
|
|
|
|
app_info_basedir = config.getoption('app_info_basedir')
|
|
|
|
app_info_filepattern = config.getoption('app_info_filepattern')
|
|
|
|
if app_info_filepattern:
|
|
|
|
apps_list = []
|
|
|
|
for file in glob.glob(os.path.join(IDF_PATH, app_info_filepattern)):
|
|
|
|
with open(file) as fr:
|
|
|
|
for line in fr.readlines():
|
|
|
|
if not line.strip():
|
|
|
|
continue
|
|
|
|
|
|
|
|
# each line is a valid json
|
|
|
|
app_info = json.loads(line.strip())
|
|
|
|
if app_info_basedir and app_info['app_dir'].startswith(app_info_basedir):
|
|
|
|
relative_app_dir = os.path.relpath(app_info['app_dir'], app_info_basedir)
|
|
|
|
apps_list.append(os.path.join(IDF_PATH, os.path.join(relative_app_dir, app_info['build_dir'])))
|
|
|
|
print('Detected app: ', apps_list[-1])
|
|
|
|
else:
|
|
|
|
print(
|
|
|
|
f'WARNING: app_info base dir {app_info_basedir} not recognizable in {app_info["app_dir"]}, skipping...'
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2023-07-31 04:49:08 +00:00
|
|
|
config.stash[IDF_PYTEST_EMBEDDED_KEY] = IdfPytestEmbedded(
|
2022-05-21 16:38:17 +00:00
|
|
|
target=target,
|
2022-03-14 03:53:36 +00:00
|
|
|
sdkconfig=config.getoption('sdkconfig'),
|
2023-05-24 02:53:57 +00:00
|
|
|
apps_list=apps_list,
|
2022-03-14 03:53:36 +00:00
|
|
|
)
|
2023-07-31 04:49:08 +00:00
|
|
|
config.pluginmanager.register(config.stash[IDF_PYTEST_EMBEDDED_KEY])
|
2022-03-14 03:53:36 +00:00
|
|
|
|
2022-11-29 09:48:36 +00:00
|
|
|
for name, description in {**TARGET_MARKERS, **ENV_MARKERS, **SPECIAL_MARKERS}.items():
|
2022-12-01 02:18:44 +00:00
|
|
|
config.addinivalue_line('markers', f'{name}: {description}')
|
|
|
|
|
2022-03-14 03:53:36 +00:00
|
|
|
|
|
|
|
def pytest_unconfigure(config: Config) -> None:
|
2023-07-31 04:49:08 +00:00
|
|
|
_pytest_embedded = config.stash.get(IDF_PYTEST_EMBEDDED_KEY, None)
|
2022-03-14 03:53:36 +00:00
|
|
|
if _pytest_embedded:
|
2023-07-31 04:49:08 +00:00
|
|
|
del config.stash[IDF_PYTEST_EMBEDDED_KEY]
|
2022-03-14 03:53:36 +00:00
|
|
|
config.pluginmanager.unregister(_pytest_embedded)
|