pytest is a Python testing framework that can be used to test your Python files. First and foremost, if you do not have pytest installed in your Python installation, pip install can be used to install pytest.
pip install pytest
And let's say you have a file named test_poc.py that contains the following. In this example, the hello function returns "World" and the test_hello function uses assert to determine if the hello function returns "World".
def hello():
return "World"
def test_hello():
assert hello() == "World"
Running test_poc.py using the pytest CLI should return the following.
~]$ pytest test_poc.py
============================================================================================ test session starts =============================================================================================
platform linux -- Python 3.12.0, pytest-9.0.1, pluggy-1.6.0
rootdir: /home/john.doe
plugins: anyio-4.11.0
collected 1 item
test_poc.py . [100%]
============================================================================================= 1 passed in 0.01s ==============================================================================================
On the other hand, let's say test_poc.py contains the following, where the test_hello functions uses assert to determine if the hello function returns "foo".
def hello():
return "World"
def test_hello():
assert hello() == "World"
Running test_poc.py using the pytest CLI should return the following, showing that the test failed.
~]$ pytest test_poc.py
============================================================================================ test session starts =============================================================================================
platform linux -- Python 3.12.0, pytest-9.0.1, pluggy-1.6.0
rootdir: /home/john.doe
plugins: anyio-4.11.0
collected 1 item
test_poc.py F [100%]
================================================================================================== FAILURES ==================================================================================================
_________________________________________________________________________________________________ test_hello _________________________________________________________________________________________________
def test_hello():
> assert hello() == "foo"
E AssertionError: assert 'World' == 'foo'
E
E - foo
E + World
test_poc.py:5: AssertionError
========================================================================================== short test summary info ===========================================================================================
FAILED test_poc.py::test_hello - AssertionError: assert 'World' == 'foo'
============================================================================================= 1 failed in 0.05s ==============================================================================================
Test a Python scripts returned code
Let's say you've Python scripts that you want to test to determine if the Python script completes successfully. For example, let's say you have a Python script named my_script.py. subprocess can be used to run the Python script and capture the return code of the Python script and any stderr returned by the Python script.
Let's say test_scripts.py contains the following which uses assert to check if my_script.py exit code is 0 (success) and if any stderr was returned.
import subprocess
command = "/usr/local/python/my_script.py"
command_args = command.split()
response = subprocess.run(command_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def test_script():
assert response.returncode == 0
assert response.stderr.decode('utf-8').strip() == ''
Did you find this article helpful?
If so, consider buying me a coffee over at 