Skip to content

Commit

Permalink
Clean up pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Urbanczyk committed Mar 9, 2024
1 parent 0ae1ed8 commit d83403d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
5 changes: 1 addition & 4 deletions scripts/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ def main():
pytest_arguments = ["--max-worker-restart=1"]

if args.trace:
os.environ["__COREBLOCKS_DUMP_TRACES"] = "1"
pytest_arguments.append("--coreblocks-traces")

if args.profile:
os.environ["__TRANSACTRON_PROFILE"] = "1"

pytest_arguments.append("--coreblocks-profile")
if args.test_name:
pytest_arguments += [f"--coreblocks-test-name={args.test_name}"]
if args.count:
Expand Down
14 changes: 14 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import os
from typing import Optional
import pytest

Expand All @@ -13,6 +14,7 @@ def pytest_addoption(parser: pytest.Parser):
help="Simulation backend for regression tests",
)
group.addoption("--coreblocks-traces", action="store_true", help="Generate traces from regression tests")
group.addoption("--coreblocks-profile", action="store_true", help="Write execution profiles")
group.addoption("--coreblocks-list", action="store_true", help="List all tests in flatten format.")
group.addoption(
"--coreblocks-test-name",
Expand Down Expand Up @@ -90,3 +92,15 @@ def deselect_based_on_count(items: list[pytest.Item], config: pytest.Config) ->
def pytest_collection_modifyitems(items: list[pytest.Item], config: pytest.Config) -> None:
deselect_based_on_flatten_name(items, config)
deselect_based_on_count(items, config)


def pytest_runtest_setup(item: pytest.Item):
"""
This function is called to perform the setup phase for every test, so
it is a perfect moment to set environment variables.
"""
if item.config.getoption("--coreblocks-traces", False): # type: ignore
os.environ["__TRANSACTRON_DUMP_TRACES"] = "1"

if item.config.getoption("--coreblocks-profile", False): # type: ignore
os.environ["__TRANSACTRON_PROFILE"] = "1"
21 changes: 8 additions & 13 deletions test/regression/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pathlib import Path
import pytest
import subprocess
import sys

test_dir = Path(__file__).parent.parent
riscv_tests_dir = test_dir.joinpath("external/riscv-tests")
Expand All @@ -19,7 +18,6 @@ def load_regression_tests() -> list[str]:
res = subprocess.run(["make", "-C", "test/external/riscv-tests"])
if res.returncode != 0:
print("Couldn't build regression tests")
sys.exit(1)
all_tests = set(get_all_test_names())

exclude = {"rv32ui-ma_data", "rv32ui-fence_i"}
Expand All @@ -28,19 +26,16 @@ def load_regression_tests() -> list[str]:


def pytest_generate_tests(metafunc: pytest.Metafunc):
if not metafunc.config.getoption("coreblocks_regression"):
# Add regression to skiped tests
metafunc.parametrize(["test_name", "backend", "traces", "verbose"], [])
return

all_tests = (
load_regression_tests()
) # The list has to be always in the same order (e.g. sorted) to allow for parallel testing
traces = metafunc.config.getoption("coreblocks_traces")
backend = metafunc.config.getoption("coreblocks_backend")
verbose = bool(metafunc.config.getoption("verbose"))
if {"test_name", "backend", "traces", "verbose"}.issubset(metafunc.fixturenames):
if "test_name" in metafunc.fixturenames:
metafunc.parametrize(
["test_name", "backend", "traces", "verbose"],
[(test_name, backend, traces, verbose) for test_name in all_tests],
"test_name",
[test_name for test_name in all_tests],
)


def pytest_runtest_setup(item: pytest.Item):
if not item.config.getoption("--coreblocks-regression", default=False): # type: ignore
pytest.skip("need --coreblocks-regression option to run this test")
24 changes: 17 additions & 7 deletions test/regression/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def regression_body_with_cocotb(test_name: str, traces: bool):
assert len(list(tree.iter("failure"))) == 0


def regression_body_with_pysim(test_name: str, traces: bool, verbose: bool):
def regression_body_with_pysim(test_name: str, traces: bool):
traces_file = None
if traces:
traces_file = REGRESSION_TESTS_PREFIX + test_name
asyncio.run(run_test(PySimulation(verbose, traces_file=traces_file), test_name))
asyncio.run(run_test(PySimulation(verbose=False, traces_file=traces_file), test_name))


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -126,8 +126,18 @@ def verilate_model(worker_id, request: pytest.FixtureRequest):
os.remove(counter_path)


def test_entrypoint(test_name: str, backend: Literal["pysim", "cocotb"], traces: bool, verbose: bool, verilate_model):
if backend == "cocotb":
regression_body_with_cocotb(test_name, traces)
elif backend == "pysim":
regression_body_with_pysim(test_name, traces, verbose)
@pytest.fixture
def sim_backend(request: pytest.FixtureRequest):
return request.config.getoption("coreblocks_backend")


@pytest.fixture
def traces_enabled(request: pytest.FixtureRequest):
return request.config.getoption("coreblocks_traces")


def test_entrypoint(test_name: str, sim_backend: Literal["pysim", "cocotb"], traces_enabled: bool, verilate_model):
if sim_backend == "cocotb":
regression_body_with_cocotb(test_name, traces_enabled)
elif sim_backend == "pysim":
regression_body_with_pysim(test_name, traces_enabled)
2 changes: 1 addition & 1 deletion transactron/testing/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def add_all_mocks(self, sim: PysimSimulator, frame_locals: dict) -> None:
@contextmanager
def run_simulation(self, module: HasElaborate, max_cycles: float = 10e4, add_transaction_module=True):
traces_file = None
if "__COREBLOCKS_DUMP_TRACES" in os.environ:
if "__TRANSACTRON_DUMP_TRACES" in os.environ:
traces_file = unittest.TestCase.id(self)

clk_period = 1e-6
Expand Down

0 comments on commit d83403d

Please sign in to comment.