Verified Commit 7c5f9996 authored by Hines, Jesse's avatar Hines, Jesse
Browse files

Better test clean up

Make sure that test output gets deleted after tests
parent 0bcabe2a
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
import pytest
import uuid
import shutil
from glob import glob
from pathlib import Path
import gc


def pytest_addoption(parser):
@@ -15,6 +19,19 @@ def pytest_runtest_setup(item):
        pytest.skip(reason)


@pytest.fixture
def random_id():
    return f"test-{str(uuid.uuid4())[:8]}"
@pytest.fixture()
def sim_output():
    """
    Handles cleaning up output from the sim.
    Can also be used even if you aren't outputing anything to run garbage collection after the sim.
    """
    out = f"test-output/test-{str(uuid.uuid4())[:8]}"
    yield out
    for file in glob(f"{out}*"):
        if Path(file).is_dir():
            shutil.rmtree(file)
        else:
            Path(file).unlink()

    # Also force a garbage collection to clean up memory after running a simulation
    gc.collect()
+1 −4
Original line number Diff line number Diff line
import gc
import pytest
from raps.engine import Engine
from raps.sim_config import SimConfig
@@ -15,7 +14,7 @@ pytestmark = [
]


def test_engine(system, system_config):
def test_engine(system, system_config, sim_output):
    if not system_config.get("main", False):
        pytest.skip(f"{system} does not support basic main run.")

@@ -35,5 +34,3 @@ def test_engine(system, system_config):

    assert engine_stats['time simulated'] == '0:02:00'
    # TODO: More specific tests of values

    gc.collect()
+2 −12
Original line number Diff line number Diff line
import os
import subprocess
import gc
import pytest
from tests.util import PROJECT_ROOT

@@ -11,7 +10,7 @@ pytestmark = [
]


def test_main_basic_run(system, system_config, random_id):
def test_main_basic_run(system, system_config, sim_output):
    if not system_config.get("main", False):
        pytest.skip(f"{system} does not support basic main run.")

@@ -20,15 +19,6 @@ def test_main_basic_run(system, system_config, random_id):
        "python", "main.py", "run",
        "--time", "1m",
        "--system", system,
        "-o", random_id
        "-o", sim_output
    ], capture_output=True, text=True, stdin=subprocess.DEVNULL)
    assert result.returncode == 0, f"Failed on {system}: {result.stderr}"

    subprocess.run(
        f"rm {random_id}.npz && rm -fr simulation_results/{random_id}",
        shell=True,
        check=True
    )

    del result
    gc.collect()
+2 −12
Original line number Diff line number Diff line
import os
import subprocess
import gc
import pytest
from tests.util import PROJECT_ROOT

@@ -12,7 +11,7 @@ pytestmark = [
]


def test_main_cooling_run(system, system_config, random_id):
def test_main_cooling_run(system, system_config, sim_output):
    if not system_config.get("cooling", False):
        pytest.skip(f"{system} does not support cooling.")

@@ -23,15 +22,6 @@ def test_main_cooling_run(system, system_config, random_id):
        "--system", system,
        "-c",
        "--noui",
        "-o", random_id
        "-o", sim_output
    ], capture_output=True, text=True, stdin=subprocess.DEVNULL)
    assert result.returncode == 0, f"Failed on {system}: {result.stderr}"

    subprocess.run(
        f"rm {random_id}.npz && rm -fr simulation_results/{random_id}",
        shell=True,
        check=True
    )

    del result
    gc.collect()
+2 −12
Original line number Diff line number Diff line
import os
import subprocess
import gc
import pytest
from tests.util import PROJECT_ROOT

@@ -12,7 +11,7 @@ pytestmark = [
]


def test_main_cooling_uncertainty_run(request, system, system_config, random_id):
def test_main_cooling_uncertainty_run(request, system, system_config, sim_output):
    print(f"Markexpr: {request.config.option.markexpr}")
    if not system_config.get("uncertainty", False) or not system_config.get("cooling", False):
        pytest.skip(f"{system} does not support cooling or uncertainty.")
@@ -25,15 +24,6 @@ def test_main_cooling_uncertainty_run(request, system, system_config, random_id)
        "-c",
        "-u",
        "--noui",
        "-o", random_id
        "-o", sim_output
    ], capture_output=True, text=True, stdin=subprocess.DEVNULL)
    assert result.returncode == 0, f"Failed on {system}: {result.stderr}"

    subprocess.run(
        f"rm {random_id}.npz && rm -fr simulation_results/{random_id}",
        shell=True,
        check=True
    )

    del result
    gc.collect()
Loading