0

I need to run pytest test_start.py and keep running the program.

My cod:

import subprocess

subprocess.run(['pytest', r'C:\Python\test_start.py'], shell=True)
print('hello')

But when I run the script, pytest starts executing and print waits for it to finish. How can I run py test and go ahead to execute the script?

UPD: When i used subprocess.Popen - I see that print has been executed, but I don't see the execution of pytest

  • 1
    The `shell=True` isn't doing anything useful here; see also [Actual meaning of `shell=True` in `subprocess`](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess). Generally, running Python as a subprocess of itself is probably something to avoid. – tripleee Mar 12 '22 at 09:31

2 Answers2

2

subprocess.run specifically waits for the process to finish. If you don't want to wait, use subprocess.Popen

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • When I used Popen, I see that print has been executed, but I don't see the execution of pytest, the browser has not opened - how do I understand that something has started there? – Идентикон Mar 12 '22 at 07:49
  • 1
    When you use bare `Popen` you take on all the responsibilities that a higher-level function lke `run` perform on your behalf, including handling any input or output (with `communicate`) and waiting on the process (with `wait`). The [tag:subprocess] tag's [info page](/tags/subprocess/info) has links to a few common FAQs about this. – tripleee Mar 12 '22 at 09:34
0

I solved this problem by simply adding time.sleep(5) after subprocess

subprocess.Popen(['pytest', r'E:\Parser\Python\test_start.py'], shell=True)
time.sleep(5)
print('hello')

Apparently pytest just didn't have time to start)

  • No, that's not how it works. By the time `Popen` returns, the process is underway. Now, if your code depends on the RESULT of the `pytest`, then you should be using `run` anyway. – Tim Roberts Mar 12 '22 at 19:27