Search code examples
pythonnpmsubprocesspostman-newman

Run newman in python


I'm trying to make a python script that runs newman on the server and saves reports, but when I run this code it says "File not found", when I put the absolute path to nmp.cmd before the command, npm doesn't see newman. Do anyone know how to make it work?

try:
   process = subprocess.Popen(['newman', 'run', '<path to the collection>', '--export-collection','<path to the export file>'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   print(f"Newman started. PID: {process.pid}")
   process.wait()
   print(f"Command completed with return code {process.returncode}")
except Exception as e:
   print(e)

With npm.cmd

try:
   process = subprocess.Popen(['C:\\Program Files\\nodejs\\npm.cmd','newman', 'run', '<path to the collection>', '--export-collection','<path to the export file>'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   print(f"Newman started. PID: {process.pid}")
   process.wait()
   print(f"Command completed with return code {process.returncode}")
except Exception as e:
   print(e)

Solution

  • Example one was correct only need to insert the parameter shell=True

    try:
       process = subprocess.Popen(['newman', 'run', '<path to the collection>', '--export-collection','<path to the export file>'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
       print(f"Newman started. PID: {process.pid}")
       process.wait()
       print(f"Command completed with return code {process.returncode}")
    except Exception as e:
       print(e)