Check console script with Pytest

less than 1 minute read

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.

Example

From the findssh package tests/test_scripts.py:

#!/usr/bin/env python
import pytest
import subprocess

def test_find():
    subprocess.check_call(['findssh'])


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

Note that I did not include the .py suffix because my setup.py:setuptools.setup() includes the argument:

entry_points={'console_scripts': ['findssh = findssh:main']},

which is useful for cross-platform uniform use–the user just types findssh no matter what platform they’re on.

Leave a comment