fix(pytest): use "--target" to filter the test cases

"-m" can't be specified for multiple times. pytest is using "and", "or",
"not" and parentheses to filter by markers
This commit is contained in:
Fu Hanxi 2021-12-02 10:14:18 +08:00
parent 5c3bc247cb
commit 61942a9f9f
2 changed files with 20 additions and 31 deletions

View File

@ -8,7 +8,7 @@
reports:
junit: XUNIT_RESULT.xml
script:
- pytest $TEST_DIR -m $TARGET_MARKER -m $ENV_MARKER --junitxml=XUNIT_RESULT.xml
- pytest $TEST_DIR --target $TARGET -m $ENV_MARKER --junitxml=XUNIT_RESULT.xml
.pytest_examples_dir_template:
extends: .pytest_template
@ -22,7 +22,7 @@ example_test_pytest_esp32_generic:
needs:
- build_examples_pytest_esp32
variables:
TARGET_MARKER: esp32
TARGET: esp32
ENV_MARKER: generic
tags: # in gitlab 14.1 or later, we can use `parallel: matrix` with the `tags` keyword. https://docs.gitlab.com/ee/ci/jobs/job_control.html#run-a-matrix-of-parallel-trigger-jobs
- ESP32
@ -35,7 +35,7 @@ example_test_pytest_esp32s2_generic:
needs:
- build_examples_pytest_esp32s2
variables:
TARGET_MARKER: esp32s2
TARGET: esp32s2
ENV_MARKER: generic
tags:
- ESP32S2
@ -48,7 +48,7 @@ example_test_pytest_esp32c3_generic:
needs:
- build_examples_pytest_esp32c3
variables:
TARGET_MARKER: esp32c3
TARGET: esp32c3
ENV_MARKER: generic
tags:
- ESP32C3

View File

@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
# pylint: disable=W0621
# pylint: disable=W0621 # redefined-outer-name
# 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.
@ -21,6 +21,7 @@ from typing import Callable, List, Optional
import pytest
from _pytest.config import Config
from _pytest.fixtures import FixtureRequest
from _pytest.nodes import Item
from pytest_embedded.plugin import parse_configuration
from pytest_embedded_idf.app import IdfApp
@ -65,34 +66,9 @@ def env_markers(pytestconfig: Config) -> List[str]:
return res
@pytest.fixture(scope='session')
def param_markers(pytestconfig: Config) -> List[str]:
res: List[str] = []
offset = -1
while True:
try:
offset = pytestconfig.invocation_params.args.index('-m', offset + 1)
except ValueError:
return res
res.append(pytestconfig.invocation_params.args[offset + 1]) # we want the marker after '-m'
@pytest.fixture
def target(request: FixtureRequest, target_markers: List[str], param_markers: List[str]) -> Optional[str]:
param_target_markers = [marker for marker in param_markers if marker in target_markers]
if len(param_target_markers) > 1:
raise ValueError('Please only specify one target marker at the same time')
elif len(param_target_markers) == 0:
target = None
else:
target = param_target_markers[0]
return getattr(request, 'param', None) or request.config.option.__dict__.get('target') or target
@pytest.fixture
def config(request: FixtureRequest) -> str:
return getattr(request, 'param', None) or request.config.option.__dict__.get('config') or 'default'
return getattr(request, 'param', None) or request.config.getoption('config', 'default') # type: ignore
@pytest.fixture
@ -149,3 +125,16 @@ def junit_properties(app: IdfApp, config: str, test_case_name: str,
This fixture is autoused and will modify the junit report test case name to <target>.<config>.<case_name>
"""
record_xml_attribute('name', format_case_id(app.target, config, test_case_name))
##################
# Hook functions #
##################
@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(config: Config, items: List[Item]) -> None:
target = config.getoption('target', None)
if not target:
return
# filter all the test cases with "--target"
items[:] = [item for item in items if target in [marker.name for marker in item.iter_markers()]]