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 600f3c0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/ragger/conftest/base_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ 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")
parser.addoption("--setup",
action="store",
default="default",
help="Specify the setup fixture (e.g., 'prod_build')",
choices=conf.OPTIONAL.ALLOWED_SETUPS + ["default"])


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -275,12 +280,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(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 600f3c0

Please sign in to comment.