0

Given the following command:

newman run tests.postman_collection.json -e environment.json  --reporters testrail,json,html

Raises:

RuntimeError: command 'newman run tests.postman_collection.json -e environment.json  --reporters testrail,json,html
' return with error (code 1): b'\nhttps://host.testrail.io/index.php?/runs/view/1234\n'

Py code that executes the command:

        try:
            newmanCLI_output = subprocess.check_output(npmCLi, shell=True).decode().strip()
        except subprocess.CalledProcessError as e:
            raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

And yes I do use the check_output return.

The output is a url to test rail reports

1 Answers1

2

That's a misfeature of os.system; it returns the exit code so you can examine it, but doesn't raise an error if something fails.

The check in subprocess.check_output means check that the command succeeded, or raise an exception otherwise. This is generally a good thing, as you don't want processes to die underneath you without a warning.

But you can work around it with subprocess.run if you want to disable it;

import shlex
result = subprocess.run(shlex.split(npmCLi), text=True, capture_output=True)
newmanCLI_output = result.stdout

The switch to avoid shell=True and use shlex.split to parse the string instead is not crucial, but hopefully demonstrates how to do these things properly.

You should still understand why exactly your command fails, and whether it is safe to ignore the failure.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • It makes sense, using ur implementation raises file not found error tho : `FileNotFoundError: [WinError 2] The system cannot find the file specified` – gabriel munteanu Nov 24 '22 at 14:56
  • Oh, Windows? How horrible. Maybe just replace the `shlex.split()` with the plain string, though still don't add `shell=True` – tripleee Nov 24 '22 at 14:57
  • Hahhaha, well it runs on windows and linux also, in ci/cd pipelines it would be on linux, didnt thought to specify tho, my bad – gabriel munteanu Nov 24 '22 at 15:01