Skip to content

Commit 7881f92

Browse files
committed
post-review: use pytest mark
1 parent 2452650 commit 7881f92

17 files changed

+197
-157
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ markers = [
224224
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
225225
"bigmem: marks tests as big memory (deselect with '-m \"not bigmem\"')",
226226
"evm_tools: marks tests as evm_tools (deselect with '-m \"not evm_tools\"')",
227+
"json_blockchain_tests: marks tests as json_blockchain_tests (deselect with '-m \"not json_blockchain_tests\"')",
228+
"json_state_tests: marks tests as json_state_tests (deselect with '-m \"not json_state_tests\"')",
229+
"vm_test: marks tests as vm_test (deselect with '-m \"not vm_test\"')",
227230
]
228231

229232
[tool.coverage.run]

tests/json_infra/conftest.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import requests_cache
99
from _pytest.config import Config
1010
from _pytest.config.argparsing import Parser
11+
from _pytest.nodes import Item
1112
from filelock import FileLock
1213
from git.exc import GitCommandError, InvalidGitRepositoryError
1314
from pytest import Session, StashKey
@@ -17,9 +18,6 @@
1718

1819
from . import TEST_FIXTURES
1920

20-
# Global variable to store pytest config for access during test collection
21-
pytest_config = None
22-
2321
try:
2422
from xdist import get_xdist_worker_id # type: ignore[import-untyped]
2523
except ImportError:
@@ -63,9 +61,6 @@ def pytest_configure(config: Config) -> None:
6361
"""
6462
Configure the ethereum module and log levels to output evm trace.
6563
"""
66-
global pytest_config
67-
pytest_config = config
68-
6964
if config.getoption("optimized"):
7065
import ethereum_optimized
7166

@@ -81,6 +76,38 @@ def pytest_configure(config: Config) -> None:
8176
ethereum.trace.set_evm_trace(Eip3155Tracer())
8277

8378

79+
def pytest_collection_modifyitems(config: Config, items: list[Item]) -> None:
80+
desired_fork = config.getoption("fork", None)
81+
if not desired_fork:
82+
return
83+
84+
selected = []
85+
deselected = []
86+
87+
for item in items:
88+
forks_of_test = [m.args[0] for m in item.iter_markers(name="fork")]
89+
if forks_of_test and desired_fork not in forks_of_test:
90+
deselected.append(item)
91+
# Check if the test has a vm test marker
92+
elif any(item.iter_markers(name="vm_test")):
93+
callspec = getattr(item, "callspec", None)
94+
if not callspec or "fork" not in getattr(callspec, "params", {}):
95+
# no fork param on this test. We keep the test
96+
selected.append(item)
97+
continue
98+
fork_param = callspec.params["fork"]
99+
if fork_param[0] == desired_fork:
100+
selected.append(item)
101+
else:
102+
deselected.append(item)
103+
else:
104+
selected.append(item)
105+
106+
if deselected:
107+
config.hook.pytest_deselected(items=deselected)
108+
items[:] = selected # keep only what matches
109+
110+
84111
class _FixturesDownloader:
85112
cache: Final[SQLiteCache]
86113
session: Final[CachedSession]

tests/json_infra/test_blockchain_tests.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pytest
44

55
from . import FORKS
6-
from .conftest import pytest_config
76
from .helpers.load_blockchain_tests import (
87
Load,
98
fetch_blockchain_tests,
@@ -13,6 +12,7 @@
1312

1413

1514
def _generate_test_function(fork_name: str) -> Callable:
15+
@pytest.mark.fork(fork_name)
1616
@pytest.mark.json_blockchain_tests
1717
@pytest.mark.parametrize(
1818
"blockchain_test_case",
@@ -29,22 +29,7 @@ def test_func(blockchain_test_case: Dict) -> None:
2929
return test_func
3030

3131

32-
# Get the fork option from pytest config if available
33-
34-
# Determine which forks to generate tests for
35-
if pytest_config and pytest_config.getoption("fork", None):
36-
# If --fork option is specified, only generate test for that fork
37-
fork_option = pytest_config.getoption("fork")
38-
if fork_option in FORKS:
39-
forks_to_test = [fork_option]
40-
else:
41-
# If specified fork is not valid, generate no tests
42-
forks_to_test = []
43-
else:
44-
# If no --fork option, generate tests for all forks
45-
forks_to_test = list(FORKS.keys())
46-
47-
for fork_name in forks_to_test:
32+
for fork_name in FORKS.keys():
4833
locals()[
4934
f"test_blockchain_tests_{fork_name.lower()}"
5035
] = _generate_test_function(fork_name)

tests/json_infra/test_state_tests.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import pytest
44

55
from . import FORKS
6-
from .conftest import pytest_config
76
from .helpers.load_state_tests import fetch_state_tests, idfn, run_state_test
87

98

109
def _generate_test_function(fork_name: str) -> Callable:
10+
@pytest.mark.fork(fork_name)
1111
@pytest.mark.evm_tools
1212
@pytest.mark.json_state_tests
1313
@pytest.mark.parametrize(
@@ -22,22 +22,7 @@ def test_func(state_test_case: Dict) -> None:
2222
return test_func
2323

2424

25-
# Get the fork option from pytest config if available
26-
27-
# Determine which forks to generate tests for
28-
if pytest_config and pytest_config.getoption("fork", None):
29-
# If --fork option is specified, only generate test for that fork
30-
fork_option = pytest_config.getoption("fork")
31-
if fork_option in FORKS:
32-
forks_to_test = [fork_option]
33-
else:
34-
# If specified fork is not valid, generate no tests
35-
forks_to_test = []
36-
else:
37-
# If no --fork option, generate tests for all forks
38-
forks_to_test = list(FORKS.keys())
39-
40-
for fork_name in forks_to_test:
25+
for fork_name in FORKS.keys():
4126
locals()[
4227
f"test_state_tests_{fork_name.lower()}"
4328
] = _generate_test_function(fork_name)

tests/json_infra/test_transaction.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from ethereum.utils.hexadecimal import hex_to_uint
1212

1313
from . import FORKS, TEST_FIXTURES
14-
from .conftest import pytest_config
1514
from .helpers.load_transaction_tests import NoTestsFound, load_test_transaction
1615

1716
ETHEREUM_TESTS_PATH = TEST_FIXTURES["ethereum_tests"]["fixture_path"]
@@ -20,6 +19,7 @@
2019

2120

2221
def _generate_high_nonce_tests_function(fork_name: str) -> Callable:
22+
@pytest.mark.fork(fork_name)
2323
@pytest.mark.parametrize(
2424
"test_file_high_nonce",
2525
[
@@ -74,20 +74,7 @@ def test_func(test_file_nonce: str) -> None:
7474
return test_func
7575

7676

77-
# Determine which forks to generate tests for
78-
if pytest_config and pytest_config.getoption("fork", None):
79-
# If --fork option is specified, only generate test for that fork
80-
fork_option = pytest_config.getoption("fork")
81-
if fork_option in FORKS:
82-
forks_to_test = [fork_option]
83-
else:
84-
# If specified fork is not valid, generate no tests
85-
forks_to_test = []
86-
else:
87-
# If no --fork option, generate tests for all forks
88-
forks_to_test = list(FORKS.keys())
89-
90-
for fork_name in forks_to_test:
77+
for fork_name in FORKS.keys():
9178
locals()[
9279
f"test_high_nonce_tests_{fork_name.lower()}"
9380
] = _generate_high_nonce_tests_function(fork_name)

tests/json_infra/vm/__init__.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from ..conftest import pytest_config
2-
31
FORKS = [
42
("ConstantinopleFix", "constantinople"),
53
("Byzantium", "byzantium"),
@@ -8,20 +6,3 @@
86
("Homestead", "homestead"),
97
("Frontier", "frontier"),
108
]
11-
12-
13-
# Determine which forks to generate tests for
14-
if pytest_config and pytest_config.getoption("fork", None):
15-
# If --fork option is specified, only generate test for that fork
16-
fork_option = pytest_config.getoption("fork")
17-
has_vm_tests = False
18-
for fork in FORKS:
19-
if fork[0] == fork_option:
20-
has_vm_tests = True
21-
forks_to_test = [fork]
22-
break
23-
if not has_vm_tests:
24-
forks_to_test = []
25-
else:
26-
# If no --fork option, generate tests for all forks
27-
forks_to_test = FORKS

tests/json_infra/vm/test_arithmetic_operations.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
from .. import TEST_FIXTURES
66
from ..helpers.load_vm_tests import VmTestLoader
7-
from . import forks_to_test
7+
from . import FORKS
88

99
ETHEREUM_TESTS_PATH = TEST_FIXTURES["ethereum_tests"]["fixture_path"]
1010
TEST_DIR = f"{ETHEREUM_TESTS_PATH}/LegacyTests/Constantinople/VMTests/vmArithmeticTest"
1111

1212

13-
@pytest.mark.parametrize("fork", forks_to_test)
13+
@pytest.mark.vm_test
14+
@pytest.mark.parametrize("fork", FORKS)
1415
@pytest.mark.parametrize(
1516
"test_file",
1617
[
@@ -28,7 +29,8 @@ def test_add(fork: Tuple[str, str], test_file: str) -> None:
2829
)
2930

3031

31-
@pytest.mark.parametrize("fork", forks_to_test)
32+
@pytest.mark.vm_test
33+
@pytest.mark.parametrize("fork", FORKS)
3234
@pytest.mark.parametrize(
3335
"test_file",
3436
[
@@ -46,7 +48,8 @@ def test_sub(fork: Tuple[str, str], test_file: str) -> None:
4648
)
4749

4850

49-
@pytest.mark.parametrize("fork", forks_to_test)
51+
@pytest.mark.vm_test
52+
@pytest.mark.parametrize("fork", FORKS)
5053
@pytest.mark.parametrize(
5154
"test_file",
5255
[
@@ -67,7 +70,8 @@ def test_mul(fork: Tuple[str, str], test_file: str) -> None:
6770
)
6871

6972

70-
@pytest.mark.parametrize("fork", forks_to_test)
73+
@pytest.mark.vm_test
74+
@pytest.mark.parametrize("fork", FORKS)
7175
@pytest.mark.parametrize(
7276
"test_file",
7377
[
@@ -88,7 +92,8 @@ def test_div(fork: Tuple[str, str], test_file: str) -> None:
8892
)
8993

9094

91-
@pytest.mark.parametrize("fork", forks_to_test)
95+
@pytest.mark.vm_test
96+
@pytest.mark.parametrize("fork", FORKS)
9297
@pytest.mark.parametrize(
9398
"test_file",
9499
[
@@ -118,7 +123,8 @@ def test_sdiv(fork: Tuple[str, str], test_file: str) -> None:
118123
)
119124

120125

121-
@pytest.mark.parametrize("fork", forks_to_test)
126+
@pytest.mark.vm_test
127+
@pytest.mark.parametrize("fork", FORKS)
122128
@pytest.mark.parametrize(
123129
"test_file",
124130
[
@@ -137,7 +143,8 @@ def test_mod(fork: Tuple[str, str], test_file: str) -> None:
137143
)
138144

139145

140-
@pytest.mark.parametrize("fork", forks_to_test)
146+
@pytest.mark.vm_test
147+
@pytest.mark.parametrize("fork", FORKS)
141148
@pytest.mark.parametrize(
142149
"test_file",
143150
[
@@ -161,7 +168,8 @@ def test_smod(fork: Tuple[str, str], test_file: str) -> None:
161168
)
162169

163170

164-
@pytest.mark.parametrize("fork", forks_to_test)
171+
@pytest.mark.vm_test
172+
@pytest.mark.parametrize("fork", FORKS)
165173
@pytest.mark.parametrize(
166174
"test_file",
167175
[
@@ -185,7 +193,8 @@ def test_addmod(fork: Tuple[str, str], test_file: str) -> None:
185193
)
186194

187195

188-
@pytest.mark.parametrize("fork", forks_to_test)
196+
@pytest.mark.vm_test
197+
@pytest.mark.parametrize("fork", FORKS)
189198
@pytest.mark.parametrize(
190199
"test_file",
191200
[
@@ -210,7 +219,8 @@ def test_mulmod(fork: Tuple[str, str], test_file: str) -> None:
210219
)
211220

212221

213-
@pytest.mark.parametrize("fork", forks_to_test)
222+
@pytest.mark.vm_test
223+
@pytest.mark.parametrize("fork", FORKS)
214224
@pytest.mark.parametrize(
215225
"test_file",
216226
[
@@ -235,7 +245,8 @@ def test_exp(fork: Tuple[str, str], test_file: str) -> None:
235245
)
236246

237247

238-
@pytest.mark.parametrize("fork", forks_to_test)
248+
@pytest.mark.vm_test
249+
@pytest.mark.parametrize("fork", FORKS)
239250
@pytest.mark.parametrize("exponent", ([2, 4, 8, 16, 32, 64, 128, 256]))
240251
def test_exp_power_2(fork: Tuple[str, str], exponent: int) -> None:
241252
VmTestLoader(*fork).run_test(
@@ -245,7 +256,8 @@ def test_exp_power_2(fork: Tuple[str, str], exponent: int) -> None:
245256
)
246257

247258

248-
@pytest.mark.parametrize("fork", forks_to_test)
259+
@pytest.mark.vm_test
260+
@pytest.mark.parametrize("fork", FORKS)
249261
def test_exp_power_256(fork: Tuple[str, str]) -> None:
250262
for i in range(1, 34):
251263
VmTestLoader(*fork).run_test(
@@ -262,7 +274,8 @@ def test_exp_power_256(fork: Tuple[str, str]) -> None:
262274
)
263275

264276

265-
@pytest.mark.parametrize("fork", forks_to_test)
277+
@pytest.mark.vm_test
278+
@pytest.mark.parametrize("fork", FORKS)
266279
@pytest.mark.parametrize(
267280
"test_file",
268281
[
@@ -288,7 +301,8 @@ def test_signextend(fork: Tuple[str, str], test_file: str) -> None:
288301
)
289302

290303

291-
@pytest.mark.parametrize("fork", forks_to_test)
304+
@pytest.mark.vm_test
305+
@pytest.mark.parametrize("fork", FORKS)
292306
def test_stop(fork: Tuple[str, str]) -> None:
293307
VmTestLoader(*fork).run_test(
294308
TEST_DIR,

0 commit comments

Comments
 (0)