Skip to content

Commit

Permalink
Add setup fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeutin-ledger committed Feb 21, 2025
1 parent cc8c4ae commit a0a3ffe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.27.0] - 2025-02-21

### Added

- Added support for different test setup

## [1.26.0] - 2025-02-06

### Added
Expand Down
33 changes: 33 additions & 0 deletions src/ragger/conftest/base_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def pytest_addoption(parser):
"ones. Will only work with 'speculos' as the backend")
parser.addoption("--log_apdu_file", action="store", default=None, help="Log the APDU in a file")
parser.addoption("--seed", action="store", default=None, help="Set a custom seed")
# Always allow "default" even if application conftest does not define it
allowed_setups = conf.OPTIONAL.ALLOWED_SETUPS
if "default" not in allowed_setups:
allowed_setups.insert(0, "default")
parser.addoption("--setup",
action="store",
default="default",
help="Specify the setup fixture (e.g., 'prod_build')",
choices=allowed_setups)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -275,12 +284,36 @@ def use_only_on_backend(request, backend_name):
pytest.skip(f'skipped on this backend: "{current_backend}"')


# This fixture looks for the 'needs_setup' marker. Example:
# @pytest.mark.needs_setup('prod_build')
@pytest.fixture(scope="function", autouse=True)
def skip_needs_setup(request):
if request.node.get_closest_marker('needs_setup'):
needed_setup = request.node.get_closest_marker('needs_setup').args[0]
else:
needed_setup = "default"
current_setup = request.config.getoption("--setup")
if needed_setup != current_setup:
pytest.skip(f"Skip test requiring setup {needed_setup} as current setup is {current_setup}")


def pytest_configure(config):
config.addinivalue_line(
"markers",
"use_on_backend(backend): skip test if not on the specified backend",
)

# fixture with parameter, use with the following syntax
# # @pytest.mark.needs_setup('prod_build')
# if not decorated, defaults to
# # @pytest.mark.needs_setup('default')
# will apply a skip filter against the "--setup <setup_name>" command line argument
# Update configuration.OPTIONAL.ALLOWED_SETUPS when adding new setups to allow the command line to accept them
config.addinivalue_line(
"markers",
"needs_setup(setup_name): skip test if not on the specified setup",
)


def log_full_conf():
logger = get_default_logger()
Expand Down
14 changes: 13 additions & 1 deletion src/ragger/conftest/configuration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Optional
from typing import Optional, List


@dataclass
Expand All @@ -10,6 +10,7 @@ class OptionalOptions:
SIDELOADED_APPS_DIR: str
BACKEND_SCOPE: str
CUSTOM_SEED: str
ALLOWED_SETUPS: List[str]


OPTIONAL = OptionalOptions(
Expand Down Expand Up @@ -48,4 +49,15 @@ class OptionalOptions:
# This would result in speculos being launched with --seed <CUSTOM_SEED>
# If a seed is provided through the "--seed" pytest command line option, it will override this one.
CUSTOM_SEED=str(),

# Use this parameter if you want ragger to handle running different test suites depending on setup
# Useful when some tests need certain build options and other tests need other build options, or a
# different Speculos command line
# Adding a setup <name> will allow you to decorate your tests with it using the following syntax
# @pytest.mark.needs_setup('<name>')
# And run marked tests and only them using the --setup <name>
#
# "default" setup is always allowed, all tests without explicit decoration depend on default
# and the --setup option defaults to "default"
ALLOWED_SETUPS=["default"],
)

0 comments on commit a0a3ffe

Please sign in to comment.