1- """Local pytest configuration for framework tests."""
1+ """Local pytest configuration used on multiple framework tests."""
22
33import os
4- from typing import Generator
4+ from typing import Dict , Generator
55
66import pytest
77
8- from ethereum_clis import ExecutionSpecsTransitionTool , TransitionTool
8+ from ethereum_clis import BesuTransitionTool , ExecutionSpecsTransitionTool , TransitionTool
99
1010
1111def pytest_runtest_setup (item ):
@@ -16,21 +16,82 @@ def pytest_runtest_setup(item):
1616 pytest .skip ("Skipping test because pytest-xdist is running with more than one worker." )
1717
1818
19- DEFAULT_T8N_FOR_UNIT_TESTS = ExecutionSpecsTransitionTool
19+ DEFAULT_TRANSITION_TOOL_FOR_UNIT_TESTS = ExecutionSpecsTransitionTool
20+
21+ INSTALLED_TRANSITION_TOOLS = [
22+ transition_tool
23+ for transition_tool in TransitionTool .registered_tools
24+ if (
25+ transition_tool .is_installed ()
26+ # Currently, Besu has the same `default_binary` as Geth, so we can't use `is_installed`.
27+ and transition_tool != BesuTransitionTool
28+ )
29+ ]
2030
2131
2232@pytest .fixture (scope = "session" )
23- def default_t8n_instance () -> Generator [TransitionTool , None , None ]:
24- """Fixture to provide a default t8n instance."""
25- instance = ExecutionSpecsTransitionTool ()
26- instance .start_server ()
27- yield instance
28- instance .shutdown ()
33+ def installed_transition_tool_instances () -> Generator [
34+ Dict [str , TransitionTool | Exception ], None , None
35+ ]:
36+ """Return all instantiated transition tools."""
37+ instances : Dict [str , TransitionTool | Exception ] = {}
38+ for transition_tool_class in INSTALLED_TRANSITION_TOOLS :
39+ try :
40+ instances [transition_tool_class .__name__ ] = transition_tool_class ()
41+ except Exception as e :
42+ # Record the exception in order to provide context when failing the appropriate test
43+ instances [transition_tool_class .__name__ ] = e
44+ yield instances
45+ for instance in instances .values ():
46+ if isinstance (instance , TransitionTool ):
47+ instance .shutdown ()
48+
49+
50+ @pytest .fixture (
51+ params = INSTALLED_TRANSITION_TOOLS ,
52+ ids = [transition_tool_class .__name__ for transition_tool_class in INSTALLED_TRANSITION_TOOLS ],
53+ )
54+ def installed_t8n (
55+ request : pytest .FixtureRequest ,
56+ installed_transition_tool_instances : Dict [str , TransitionTool | Exception ],
57+ ) -> TransitionTool :
58+ """
59+ Return an instantiated transition tool.
60+
61+ Tests using this fixture will be automatically parameterized with all
62+ installed transition tools.
63+ """
64+ transition_tool_class = request .param
65+ assert issubclass (transition_tool_class , TransitionTool )
66+ assert transition_tool_class .__name__ in installed_transition_tool_instances , (
67+ f"{ transition_tool_class .__name__ } not instantiated"
68+ )
69+ instance_or_error = installed_transition_tool_instances [transition_tool_class .__name__ ]
70+ if isinstance (instance_or_error , Exception ):
71+ raise Exception (
72+ f"Failed to instantiate { transition_tool_class .__name__ } "
73+ ) from instance_or_error
74+ return instance_or_error
2975
3076
3177@pytest .fixture
3278def default_t8n (
33- default_t8n_instance : TransitionTool ,
79+ installed_transition_tool_instances : Dict [ str , TransitionTool | Exception ] ,
3480) -> TransitionTool :
3581 """Fixture to provide a default t8n instance."""
36- return default_t8n_instance
82+ instance = installed_transition_tool_instances .get (
83+ DEFAULT_TRANSITION_TOOL_FOR_UNIT_TESTS .__name__
84+ )
85+ if instance is None :
86+ raise Exception (f"Failed to instantiate { DEFAULT_TRANSITION_TOOL_FOR_UNIT_TESTS .__name__ } " )
87+ if isinstance (instance , Exception ):
88+ raise Exception (
89+ f"Failed to instantiate { DEFAULT_TRANSITION_TOOL_FOR_UNIT_TESTS .__name__ } "
90+ ) from instance
91+ return instance
92+
93+
94+ @pytest .fixture (scope = "session" )
95+ def running_in_ci () -> bool :
96+ """Return whether the test is running in a CI environment."""
97+ return "CI" in os .environ
0 commit comments