开发者

creating testsuite in python

开发者 https://www.devze.com 2023-03-03 20:39 出处:网络
I have python scripts, such as a.py, b.py开发者_Go百科, c.py located in say directory A: I want to create test suite which will execute all those python scripts sequentially.

I have python scripts, such as a.py, b.py开发者_Go百科, c.py located in say directory A:

I want to create test suite which will execute all those python scripts sequentially. Those scripts are no a unit test. Those are regular python scripts.

Please let me know how do I run them in test suite and generate report after execution.

Thanks for your time


I think what you want is actually done on the shell, or command line if you are on Windows. You can loop through each file and execute each script in order. You are then able to have the actual generate a report to save in a file after they are done processing, by using the open() command in python.

The syntax of the bash script would be,

for file in /path/to/dir_with_files/*.py;
do;
python $file;
done;

That will loop through the file with the programs and execute each one ending in a .py extension.


Now, to send a report to yourself after all have been tested and executed with the above script. You will need to do 2 things to do this,

  1. Create a textfile with the results of the tests
  2. Send it to yourself through an email lib

Number 1 is the easy part. To do this, you just want to write the results of each test to a file, like so,

results = open('results.txt', 'w')

results.write('results') #This is going to need to be what the results of
                         #the test are, I.E. the numbers it produces, or such

results.close()

You will just need to add that to the end of each of the scripts, to create a text file that can than be sent using step 2.

Step 2 is a bit more tricky. To do this, you'll need one of 2 things, a module to interact with whichever mail client you use, (gmail, comcast, yahoo, etc), or your own SMTP server to send the mail from there. If you use the latter, Python has a built in module to deal with that, documented here.

Now, if you want to interact with your mail client, you will need to download a module and install said module to use. For example, gmail has this, which logs you into their service and will send a message through their server. I'm sure that most popular email clients will have a module like this, so just google around for it. This is much easier than method 1, as you do not need an SMTP server.


You can also write an other python file like this:

import subprocess as sb # use shell cmds
import os # list directory

file_output = open("myOutput.txt", "w") # output file

for f in os.listdir("path/to/the/directory"): # you can use "."
    if (f.endswith(".py")): # for each python file
        file_output.write("Execution of "+f+"\n\n") # write the name
        file_output.write(sb.getoutput("python "+f)+"\n\n") # write output
file_output.close()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号