running Matlab from Pytest

A software package may have Matlab and Python functions. To test the Matlab functions from Pytest, create a file like:

from pathlib import Path
import subprocess
import pytest
import shutil

R = Path(__file__).parent

OCTAVE = shutil.which('octave-cli')
MATLAB = shutil.which('matlab')

@pytest.mark.skipif(not MATLAB, reason="Matlab not available")
def test_matlab_api():
    subprocess.check_call([MATLAB, '-batch', 'test_myscript'],
                          cwd=R, timeout=60)

@pytest.mark.skipif(not OCTAVE, reason='octave not found')
def test_octave_api():
    subprocess.check_call([OCTAVE, 'test_myscript.m'],
                          cwd=R, timeout=60)

if __name__ == '__main__':
    pytest.main(['-xrsv', __file__])

This assumes the Matlab test script resides in the same directory as the file, and is named like test_api.m.