Skip to content

Commit 53e92c6

Browse files
committed
fix(tests): Don't cache fixtures
1 parent 14a8f11 commit 53e92c6

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

tests/json_infra/helpers/fixtures.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@ class Fixture(ABC):
1818

1919
test_file: str
2020
test_key: str
21-
test_dict: Dict[str, Any]
2221

2322
def __init__(
2423
self,
2524
*args: Any,
2625
test_file: str,
2726
test_key: str,
28-
test_dict: Dict[str, Any],
2927
**kwargs: Any,
3028
):
3129
super().__init__(*args, **kwargs)
3230
self.test_file = test_file
3331
self.test_key = test_key
34-
self.test_dict = test_dict
3532

3633
@classmethod
3734
def from_parent(

tests/json_infra/helpers/load_blockchain_tests.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Helpers to load and run blockchain tests from JSON files."""
22

33
import importlib
4+
import json
45
from pathlib import Path
56
from typing import Any, Dict, Tuple
67
from unittest.mock import call, patch
@@ -67,11 +68,12 @@ class BlockchainTestFixture(Fixture, Item):
6768
def __init__(
6869
self,
6970
*args: Any,
71+
test_dict: Dict[str, Any],
7072
**kwargs: Any,
7173
) -> None:
7274
"""Initialize a single blockchain test fixture from a JSON file."""
7375
super().__init__(*args, **kwargs)
74-
self.fork_name = self.test_dict["network"]
76+
self.fork_name = test_dict["network"]
7577
self.add_marker(pytest.mark.fork(self.fork_name))
7678
self.add_marker("evm_tools")
7779
self.add_marker("json_blockchain_tests")
@@ -91,9 +93,15 @@ def __init__(
9193
if any(x.search(_identifier) for x in test_patterns.big_memory):
9294
self.add_marker("bigmem")
9395

96+
def test_dict(self) -> Dict[str, Any]:
97+
"""Load test from disk."""
98+
with open(self.test_file, "r") as file:
99+
loaded_file = json.load(file)
100+
return loaded_file[self.test_key]
101+
94102
def runtest(self) -> None:
95103
"""Run a blockchain state test from JSON test case data."""
96-
json_data = self.test_dict
104+
json_data = self.test_dict()
97105
if "postState" not in json_data:
98106
pytest.xfail(
99107
f"{self.test_file}[{self.test_key}] doesn't have post state"

tests/json_infra/helpers/load_state_tests.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,16 @@ class StateTest(Item):
2828
"""Single state test case item."""
2929

3030
test_case: TestCase
31-
test_dict: Dict[str, Any]
3231

3332
def __init__(
3433
self,
3534
*args: Any,
3635
test_case: TestCase,
37-
test_dict: Dict[str, Any],
3836
**kwargs: Any,
3937
) -> None:
4038
"""Initialize a single test case item."""
4139
super().__init__(*args, **kwargs)
4240
self.test_case = test_case
43-
self.test_dict = test_dict
4441
self.add_marker(pytest.mark.fork(self.test_case.fork_name))
4542
self.add_marker("evm_tools")
4643
self.add_marker("json_state_tests")
@@ -51,13 +48,19 @@ def __init__(
5148
if any(x.search(test_case.key) for x in test_patterns.slow):
5249
self.add_marker("slow")
5350

51+
def test_dict(self) -> Dict[str, Any]:
52+
"""Load test from disk."""
53+
with open(self.test_case.path, "r") as file:
54+
loaded_file = json.load(file)
55+
return loaded_file[self.test_case.key]
56+
5457
def runtest(self) -> None:
5558
"""
5659
Runs a single general state test.
5760
"""
5861
index = self.test_case.index
5962
json_fork = self.test_case.fork_name
60-
test_dict = self.test_dict
63+
test_dict = self.test_dict()
6164

6265
env = test_dict["env"]
6366
try:
@@ -131,6 +134,19 @@ class StateTestFixture(Fixture, Collector):
131134
cases.
132135
"""
133136

137+
_test_dict: Dict[str, Any] | None
138+
139+
def __init__(
140+
self,
141+
*args: Any,
142+
test_dict: Dict[str, Any],
143+
**kwargs: Any,
144+
) -> None:
145+
"""Initialize a single blockchain test fixture from a JSON file."""
146+
super().__init__(*args, **kwargs)
147+
# Save for collection purposes only, then free.
148+
self._test_dict = test_dict
149+
134150
@classmethod
135151
def is_format(cls, test_dict: Dict[str, Any]) -> bool:
136152
"""Return true if the object can be parsed as the fixture type."""
@@ -146,10 +162,12 @@ def is_format(cls, test_dict: Dict[str, Any]) -> bool:
146162

147163
def collect(self) -> Iterable[Item | Collector]:
148164
"""Collect state test cases inside of this fixture."""
165+
assert self._test_dict is not None
166+
test_dict, self._test_dict = self._test_dict, None
149167
for test_case in read_test_case(
150168
test_file_path=self.test_file,
151169
key=self.test_key,
152-
test=self.test_dict,
170+
test=test_dict,
153171
):
154172
if test_case.fork_name not in FORKS:
155173
continue
@@ -158,5 +176,4 @@ def collect(self) -> Iterable[Item | Collector]:
158176
parent=self,
159177
name=name,
160178
test_case=test_case,
161-
test_dict=self.test_dict,
162179
)

0 commit comments

Comments
 (0)