flexops.testing

The public, shipped test harness for unit models. Every milestone that adds a unit model, and anyone writing a custom one, subclasses UnitModelTestHarness instead of writing the build/units-consistency/registration/DoF/solve checks by hand.

This module imports pytest, so it requires the testing extra. Install it with pip install "flex-pse[testing]" (already covered by the dev extra).

UnitModelTestHarness

Subclass per unit model; pytest collects the subclass.

flexops.testing.dummy_time_block(n: int = 3) → ConcreteModel

Build a throwaway model with an n-point TimeBlock + properties.

Reused by the M14 docs generator to construct a unit model for rendering its Variables/Constraints/Degrees-of-Freedom tables.

Parameters:

n – Number of 15-minute time points to span.

Returns:

A pyo.ConcreteModel with m.time_block (an n-point, 15-minute-resolution TimeBlock starting 2025-01-01) and m.properties (a SimpleAqueousFlow()).

flexops.testing.dummy_gas_time_block(n: int = 3) → ConcreteModel

Build a throwaway model with an n-point TimeBlock + gas properties.

The gas twin of dummy_time_block(), for unit models built on SimpleGasFlow. Its state blocks always carry three state variables (flow, pressure, temperature), where the aqueous package’s default carries one, so a gas unit’s degree-of-freedom accounting differs and needs this fixture rather than the aqueous one.

Parameters:

n – Number of 15-minute time points to span.

Returns:

A pyo.ConcreteModel with m.time_block (an n-point, 15-minute-resolution TimeBlock starting 2025-01-01) and m.properties (a SimpleGasFlow()).

Testing your own unit model

A concrete unit’s test file is about 30 lines long, just configure() plus the two dicts of expected values.

from flexops.testing import UnitModelTestHarness, dummy_time_block
from flexops.unit_models import Pump


class TestPump(UnitModelTestHarness):
    """Fixed inlet flow determines power_electrical via energy_intensity."""

    expected_dof = 0
    expected_solution = {
        "power_electrical[0]": 50.0,
        "power_electrical[1]": 50.0,
        "power_electrical[2]": 50.0,
    }

    def configure(self):
        m = dummy_time_block(3)
        m.unit = Pump(property_package=m.properties)
        for t in m.time_block.time_index:
            m.unit.inlet_state.flow_vol_phase[t, "Liq"].set_value(100.0)
        return m, m.unit