From afb62eebf0f4c9d10abfc620b87f4f3492f024a3 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 18 Oct 2021 17:42:47 +0800 Subject: [PATCH] Add pytest CLI option `--fork` so that we can just run with specific phase (fork) --- tests/core/pyspec/eth2spec/test/conftest.py | 20 ++++++++++++++++++++ tests/core/pyspec/eth2spec/test/context.py | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/core/pyspec/eth2spec/test/conftest.py b/tests/core/pyspec/eth2spec/test/conftest.py index e6d8352e0..55613be0a 100644 --- a/tests/core/pyspec/eth2spec/test/conftest.py +++ b/tests/core/pyspec/eth2spec/test/conftest.py @@ -1,4 +1,7 @@ from eth2spec.test import context +from eth2spec.test.helpers.constants import ( + ALL_PHASES, +) from eth2spec.utils import bls as bls_utils # We import pytest only when it's present, i.e. when we are running tests. @@ -29,6 +32,13 @@ def pytest_addoption(parser): "--preset", action="store", type=str, default="minimal", help="preset: make the pyspec use the specified preset" ) + parser.addoption( + "--fork", action="append", type=str, + help=( + "fork: make the pyspec only run with the specified phase." + " To run multiple phases, e.g., --fork=phase0 --fork=altair" + ) + ) parser.addoption( "--disable-bls", action="store_true", default=False, help="bls-default: make tests that are not dependent on BLS run without BLS" @@ -44,6 +54,16 @@ def preset(request): context.DEFAULT_TEST_PRESET = request.config.getoption("--preset") +@fixture(autouse=True) +def run_phases(request): + phases = request.config.getoption("--fork") + if phases: + phases = [phase.lower() for phase in phases] + context.DEFAULT_PYTEST_FORKS = set(phases) + else: + context.DEFAULT_PYTEST_FORKS = ALL_PHASES + + @fixture(autouse=True) def bls_default(request): disable_bls = request.config.getoption("--disable-bls") diff --git a/tests/core/pyspec/eth2spec/test/context.py b/tests/core/pyspec/eth2spec/test/context.py index 6062648d6..944351bfd 100644 --- a/tests/core/pyspec/eth2spec/test/context.py +++ b/tests/core/pyspec/eth2spec/test/context.py @@ -22,6 +22,9 @@ from lru import LRU # Without pytest CLI arg or pyspec-test-generator 'preset' argument, this will be the config to apply. DEFAULT_TEST_PRESET = MINIMAL +# Without pytest CLI arg or pyspec-test-generator 'run-phase' argument, this will be the config to apply. +DEFAULT_PYTEST_FORKS = ALL_PHASES + # TODO: currently phases are defined as python modules. # It would be better if they would be more well-defined interfaces for stronger typing. @@ -351,7 +354,7 @@ def with_phases(phases, other_phases=None): """ def decorator(fn): def wrapper(*args, **kw): - run_phases = phases + run_phases = set(phases).intersection(DEFAULT_PYTEST_FORKS) # limit phases if one explicitly specified if 'phase' in kw: