Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a set of Python scripts in a list

Tags:

python

I am working on a Python project that includes a lot of simple example scripts to help new users get used to the system. As well as the source code for each example, I include the output I get on my test machine so users know what to expect when all goes well.

It occured to me that I could use this as a crude form of unit testing. Automatically run all the example scripts and do a load of diffs against the expected output.

All of my example scripts end with extension .py so I can get their filenames easily enough with something like

pythonfiles=[filename for filename in os.listdir(source_directory) if filename[-3:]=='.py']

So, pythonfiles contains something like ['example1.py', 'cool_example.py'] and so on.

What syntax can I use to actually run the scripts referenced in this list?

like image 305
WalkingRandomly Avatar asked Jun 11 '26 20:06

WalkingRandomly


2 Answers

You could leverage doctest to help you get this done. Write a method that executes each script, and in the docstring for each method you paste the expected output:

def run_example1():
    """
    This is example number 1. Running it should give you the following output:

    >>> run_example1()
    "This is the output from example1.py"
    """

    os.system('python example1.py') # or you could use subprocess here

if __name__ == "__main__":
    import doctest
    doctest.testmod()

Note I haven't tested this.

Alternatively, as Shane mentioned, you could use subprocess. Something like this will work:

import subprocess

cmd = ('example1.py', 'any', 'more', 'arguments')

expected_out = """Your expected output of the script"""

exampleP = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = exampleP.communicate() # out and err are stdout and stderr, respectively

if out != expected_out:
    print "Output does not match"
like image 199
Mike Mazur Avatar answered Jun 13 '26 09:06

Mike Mazur


You want to use the subprocess module.

like image 22
Shane C. Mason Avatar answered Jun 13 '26 09:06

Shane C. Mason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!