|
| 1 | +"""Test the benchmarking pytest plugin for gas benchmark values.""" |
| 2 | + |
| 3 | +import textwrap |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +test_module_dummy = textwrap.dedent( |
| 8 | + """\ |
| 9 | + import pytest |
| 10 | +
|
| 11 | + from ethereum_test_tools import Environment |
| 12 | +
|
| 13 | + @pytest.mark.valid_at("Istanbul") |
| 14 | + def test_dummy_benchmark_test(state_test, gas_benchmark_value): |
| 15 | + state_test( |
| 16 | + env=env,pre={},post={},tx=None) |
| 17 | + """ |
| 18 | +) |
| 19 | + |
| 20 | +test_module_without_fixture = textwrap.dedent( |
| 21 | + """\ |
| 22 | + import pytest |
| 23 | +
|
| 24 | + from ethereum_test_tools import Environment |
| 25 | +
|
| 26 | + @pytest.mark.valid_at("Istanbul") |
| 27 | + def test_dummy_no_benchmark_test(state_test): |
| 28 | + state_test(env=env, pre={}, post={}, tx=None) |
| 29 | + """ |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +def setup_test_directory_structure( |
| 34 | + pytester: pytest.Pytester, test_content: str, test_filename: str |
| 35 | +): |
| 36 | + """ |
| 37 | + Set up the common test directory structure used across multiple tests. |
| 38 | +
|
| 39 | + Args: |
| 40 | + pytester: The pytest Pytester fixture |
| 41 | + test_content: The content to write to the test file |
| 42 | + test_filename: The name of the test file to create |
| 43 | +
|
| 44 | + Returns: |
| 45 | + The path to the created test module file |
| 46 | +
|
| 47 | + """ |
| 48 | + tests_dir = pytester.mkdir("tests") |
| 49 | + istanbul_tests_dir = tests_dir / "istanbul" |
| 50 | + istanbul_tests_dir.mkdir() |
| 51 | + dummy_dir = istanbul_tests_dir / "dummy_test_module" |
| 52 | + dummy_dir.mkdir() |
| 53 | + test_module = dummy_dir / test_filename |
| 54 | + test_module.write_text(test_content) |
| 55 | + |
| 56 | + pytester.copy_example(name="src/cli/pytest_commands/pytest_ini_files/pytest-fill.ini") |
| 57 | + |
| 58 | + return test_module |
| 59 | + |
| 60 | + |
| 61 | +def test_gas_benchmark_option_added(pytester: pytest.Pytester): |
| 62 | + """Test that the --gas-benchmark-values option is properly added.""" |
| 63 | + pytester.copy_example(name="src/cli/pytest_commands/pytest_ini_files/pytest-fill.ini") |
| 64 | + |
| 65 | + # Command: pytest -p pytest_plugins.filler.benchmarking --help |
| 66 | + result = pytester.runpytest("-c", "pytest-fill.ini", "--help") |
| 67 | + |
| 68 | + assert result.ret == 0 |
| 69 | + assert any("--gas-benchmark-values" in line for line in result.outlines) |
| 70 | + assert any("Specify gas benchmark values for tests" in line for line in result.outlines) |
| 71 | + |
| 72 | + |
| 73 | +def test_benchmarking_mode_configured_with_option(pytester: pytest.Pytester): |
| 74 | + """Test that fill_mode is set to BENCHMARKING when --gas-benchmark-values is used.""" |
| 75 | + setup_test_directory_structure(pytester, test_module_dummy, "test_dummy_benchmark.py") |
| 76 | + |
| 77 | + # Test with gas benchmark values |
| 78 | + result = pytester.runpytest( |
| 79 | + "-c", |
| 80 | + "pytest-fill.ini", |
| 81 | + "--fork", |
| 82 | + "Istanbul", |
| 83 | + "--gas-benchmark-values", |
| 84 | + "10,20,30", |
| 85 | + "tests/istanbul/dummy_test_module/", |
| 86 | + "--collect-only", |
| 87 | + "-q", |
| 88 | + ) |
| 89 | + |
| 90 | + assert result.ret == 0 |
| 91 | + assert any("9 tests collected" in line for line in result.outlines) |
| 92 | + # Check that the test names include the benchmark gas values |
| 93 | + assert any("benchmark-gas-value_10M" in line for line in result.outlines) |
| 94 | + assert any("benchmark-gas-value_20M" in line for line in result.outlines) |
| 95 | + assert any("benchmark-gas-value_30M" in line for line in result.outlines) |
| 96 | + |
| 97 | + |
| 98 | +def test_benchmarking_mode_not_configured_without_option(pytester: pytest.Pytester): |
| 99 | + """Test that fill_mode is not set to BENCHMARKING when --gas-benchmark-values is not used.""" |
| 100 | + setup_test_directory_structure(pytester, test_module_dummy, "test_dummy_benchmark.py") |
| 101 | + |
| 102 | + # Test without gas benchmark values |
| 103 | + result = pytester.runpytest( |
| 104 | + "-c", |
| 105 | + "pytest-fill.ini", |
| 106 | + "--fork", |
| 107 | + "Istanbul", |
| 108 | + "tests/istanbul/dummy_test_module/", |
| 109 | + "--collect-only", |
| 110 | + "-q", |
| 111 | + ) |
| 112 | + |
| 113 | + assert result.ret == 0 |
| 114 | + # Should generate normal test variants (3) without parametrization |
| 115 | + assert any("3 tests collected" in line for line in result.outlines) |
| 116 | + assert not any("benchmark-gas-value_10M" in line for line in result.outlines) |
| 117 | + assert not any("benchmark-gas-value_20M" in line for line in result.outlines) |
| 118 | + assert not any("benchmark-gas-value_30M" in line for line in result.outlines) |
0 commit comments