Skip to content

Commit cfddac5

Browse files
authored
feat(plugins/forks): Custom covariant function decorators (#1019)
1 parent ca91240 commit cfddac5

File tree

7 files changed

+417
-129
lines changed

7 files changed

+417
-129
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Test fixtures for use by clients are available for each release on the [Github r
6464
- ✨ Generate Transaction Test type ([#933](https://github.com/ethereum/execution-spec-tests/pull/933)).
6565
- ✨ Add a default location for evm logs (`--evm-dump-dir`) when filling tests ([#999](https://github.com/ethereum/execution-spec-tests/pull/999)).
6666
- ✨ Slow tests now have greater timeout when making a request to the T8N server ([#1037](https://github.com/ethereum/execution-spec-tests/pull/1037)).
67+
- ✨ Introduce [`fork_covariant_parametrize`](https://ethereum.github.io/execution-spec-tests/main/writing_tests/test_markers/#custom-fork-covariant-markers) helper function ([#1019](https://github.com/ethereum/execution-spec-tests/pull/1019)).
6768

6869
### 🔧 EVM Tools
6970

docs/writing_tests/test_markers.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,41 @@ def test_something_with_all_tx_types_but_skip_type_1(state_test_only, tx_type):
271271

272272
In this example, the test will be skipped if `tx_type` is equal to 1 by returning a `pytest.mark.skip` marker, and return `None` otherwise.
273273

274+
## Custom Fork Covariant Markers
275+
276+
Custom fork covariant markers can be created by using the `fork_covariant_parametrize` decorator.
277+
278+
This decorator takes three arguments:
279+
280+
- `parameter_names`: A list of parameter names that will be parametrized using the custom function.
281+
- `fn`: A function that takes the fork as parameter and returns a list of values that will be used to parametrize the test.
282+
- `marks`: A marker, list of markers, or a lambda function that can be used to add additional markers to the test.
283+
284+
```python
285+
import pytest
286+
287+
from pytest_plugins import fork_covariant_parametrize
288+
289+
def covariant_function(fork):
290+
return [[1, 2], [3, 4]] if fork.name() == "Paris" else [[4, 5], [5, 6], [6, 7]]
291+
292+
@fork_covariant_parametrize(parameter_names=[
293+
"test_parameter", "test_parameter_2"
294+
], fn=covariant_function)
295+
@pytest.mark.valid_from("Paris")
296+
@pytest.mark.valid_until("Shanghai")
297+
def test_case(state_test_only, test_parameter, test_parameter_2):
298+
pass
299+
```
300+
301+
In this example, the test will be parametrized with the values `[1, 2]` and `[3, 4]` for the Paris fork, with values `1` and `3` being assigned to `test_parameter` and `2` and `4` being assigned to `test_parameter_2`. For the Shanghai fork, the test will be parametrized with the values `[4, 5]`, `[5, 6]`, and `[6, 7]`. Therefore, more test cases will be generated for the Shanghai fork.
302+
303+
If the parameters that are being parametrized is only a single parameter, the return value of `fn` should be a list of values for that parameter.
304+
305+
If the parameters that are being parametrized are multiple, the return value of `fn` should be a list of tuples/lists, where each tuple contains the values for each parameter.
306+
307+
The function can also return a list of `pytest.param` objects, which allows for additional markers and test IDs to be added to the test.
308+
274309
## Fill/Execute Markers
275310

276311
These markers are used to apply different markers to a test depending on whether it is being filled or executed.

src/pytest_plugins/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
"""Package containing pytest plugins related to test filling."""
2+
3+
from .forks import fork_covariant_parametrize
4+
5+
__all__ = ["fork_covariant_parametrize"]

src/pytest_plugins/forks/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
tests based on the user-provided fork range the tests' specified validity
44
markers.
55
"""
6+
7+
from .forks import fork_covariant_parametrize
8+
9+
__all__ = ["fork_covariant_parametrize"]

0 commit comments

Comments
 (0)