2

I have a script which is an automated test. I need to be able to set a run to be X cycles long (or even infinite). What is the best method of doing so ?

By the way, currently I am using my IDE to run the whole script and sometimes I use the CLI to run certain code chunks. Which will be needed for my needs

Moshe S.
  • 2,834
  • 3
  • 19
  • 31

2 Answers2

2

Using python, if you want to loop code from an other script (GuiTest.py) without ever modifying it you have two solutions depending on the situation:

1.You know nothing about the script and just know that it can be ran:

import os 
for i in range(X):
    os.system("python GuiTest.py")

That will launch a brand new python interpreter and run the script as you would do it by hand

2.You know how the code looks like and what you want to loop is enclosed in a function or a few functions

import GuiTest
for i in range(X):
    GuiTest.function_to_loop()
    #if you have to run more than one function per loop:
    #GuiTest.other_function_to_loop()

This has the advantage of initializing the script only once (doing the imports etc...) and then only loop the actual code. With selenium it can be interesting because the browser can take a while to start.

jadsq
  • 3,033
  • 3
  • 20
  • 32
  • I need this king of a loop to be set during the run and not coded inside the script. If for instance I have "GuiTest.py" (which doesn't include a loop inside it, a loop that makes it run several times in a row) and I want to run it from the CLI and set the run to cycle this *.py file several times. This is what I meant – Moshe S. Jun 21 '17 at 09:46
0

python -c 'for i in range(10): print "hello"'

tested.

or your shell

for i in `seq 10`;do echo hello; done

superK
  • 3,932
  • 6
  • 30
  • 54
  • Can I use the "python -c " for running a *.py file ? – Moshe S. Jun 21 '17 at 09:45
  • @MosheS. You could but without python -c, there is a bit change,`echo -e 'import os\nfor i in range(10):os.system("./GuiTest.py")' | python ` Why can't we use python -c to direct call script, see [here](https://stackoverflow.com/questions/25211865/using-an-import-and-a-for-loop-when-passing-a-program-as-a-string-to-python) – superK Jun 21 '17 at 10:13