0

For a python pytest file with contents of:

import os
def test_that():
    print("step 1: should work")
    os.system("echo hello")
    
    print("step 2: should fail")
    os.system("asdf_failing_non_existing_command")

and an invocation of pytest like:

 pytest -s testing.py

The output is:

1 passed

I would expect the test to fail. How can I get the exit code from os system call to influence the test failure and actually fail the test?

Georg Heiler
  • 16,916
  • 36
  • 162
  • 292

3 Answers3

1

How can I get the exit code from os system call to influence the test failure and actually fail the test?

Don't use os.system. (You almost never need it, and the docs are pretty strong on using subprocess instead).

With subprocess:

import subprocess

subprocess.run(["echo", "hi"], check=True)
subprocess.run(["asdfasdfasdfdas"], check=True)

Or you can use os.system and check yourself, but why reinvent the wheel?

2e0byo
  • 5,305
  • 1
  • 6
  • 26
1

You can assert that the result of os.system() is equal to 0:

import os
def test_that():
    print("step 1: should work")
    assert os.system("echo hello") == 0
    
    print("step 2: should fail")
    assert os.system("asdf_failing_non_existing_command") == 0

The output is:

    def test_that():
        print("step 1: should work")
        assert os.system("echo hello") == 0

        print("step 2: should fail")
>       assert os.system("asdf_failing_non_existing_command") == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <built-in function system>('asdf_failing_non_existing_command')
E        +    where <built-in function system> = os.system

test.py:7: AssertionError
============================================================================== short test summary info =============================================================================== 
FAILED test.py::test_that - AssertionError: assert 1 == 0
================================================================================= 1 failed in 0.16s ==================================================================================
Yannis Sauzeau
  • 396
  • 1
  • 11
0

Use subprocess.getstatusoutput, just raise an Exception if the first item it returns (the return code) isn't 0.

Ludwig
  • 328
  • 1
  • 6