pymc.testing.mock_sample_setup_and_teardown#

pymc.testing.mock_sample_setup_and_teardown()[source]#

Set up and tear down mocking of PyMC sampling functions for testing.

This function is designed to be used with pytest fixtures to temporarily replace PyMC’s sampling functionality with faster alternatives for testing purposes.

Effects during the fixture’s active period:

Examples

Use with pytest to mock actual PyMC sampling in test suite.

# tests/conftest.py
import pytest
import pymc as pm
from pymc.testing import mock_sample_setup_and_teardown

# Register as a pytest fixture
mock_pymc_sample = pytest.fixture(scope="function")(mock_sample_setup_and_teardown)

# tests/test_model.py
# Use in a test function
def test_model_inference(mock_pymc_sample):
    with pm.Model() as model:
        x = pm.Normal("x", 0, 1)
        # This will use mock_sample instead of actual MCMC
        idata = pm.sample()
        # Test with the inference data...