Check console script with Pytest
Pytest is the de facto standard for Python unit testing and continuous integration. To be complete in testing, one should test the interactive console scripts that for many Python programs is the main method of use.
Console script testing can be added through
Pytest Console Scripts
addon, but I usually simply use subprocess.check_call
directly like Pytest Console Scripts addon does.
Note that “sys.executable” is the recommended way to securely get the Python executable path, to ensure testing with the same Python interpreter.
import pytest
import subprocess
import sys
def test_find():
subprocess.check_call([sys.executable, '-m', 'mypkg'])
This is for a package configured with __main__.py
or __init__.py
such that in normal use, the user types:
python -m mypkg
to run the Python program.