views:

70

answers:

2

I am using Selenium to run tests on a website. I have many individual test I need to run, and want to create a script that will run all of the python files in a certain folder. I can get the names, and import the modules, but once I do this I can't get the unittest to run the files. Here is some of the test code I have created. My problem seems to be that once I glob the names they are input as strings, and I can't get away from it.

I want to write one of these files for each folder, or some way of executing all of the folders in a directory. Here is the code I have so far:

\## This Module will execute all of the Admin>Vehicles>Add Vehicle (AVHC) Test Cases
import sys, glob, unittest

sys.path.append('/com/inthinc/python/tiwiPro/IE7/AVHC')
AddVehicle_IE7_tests = glob.glob('/com/inthinc/python/tiwipro/IE7/AVHC/*.py')

for i in range( len(AddVehicle_IE7_tests) ):
        replaceme = AddVehicle_IE7_tests[i]
        withoutpy = replaceme.replace( '.py', '')
        withouttree1 = withoutpy.replace( '/com/inthinc/python/tiwipro/IE7/AVHC\\', '' )
        exec("import " + withouttree1)
        AddVehicle_IE7_tests[i] = withouttree1

sys.path.append('/com/inthinc/python/tiwiPro/FF3/AVHC')
AddVehicle_FF3_tests = glob.glob('/com/inthinc/python/tiwipro/FF3/AVHC/*.py')

for i in range( len(AddVehicle_FF3_tests) ):
        replaceme = AddVehicle_FF3_tests[i]
        withoutpy = replaceme.replace( '.py', '')
        withouttree2 = withoutpy.replace( '/com/inthinc/python/tiwipro/FF3/AVHC\\', '' )
        exec("import " + withouttree2)
        print withouttree2


if __name__ == '__main__':
        print AddVehicle_IE7_tests
        unittest.TextTestRunner().run(AddVehicle_IE7_tests)
else:
        unittest.TextTestRunner().run(AddVehicle_IE7_tests)
        unittest.TextTestRunner().run(AddVehicle_FF3_tests)
        print "success"
A: 

Although I wouldn't exactly recommend this approach (nor probably what you're trying to do), here's a simple approach that appears to accomplish roughly what you want.

In file "runner.py" (for example, similar to your above): import glob import unittest

testfiles = glob.glob('subdir/*.py')
for name in testfiles:
    execfile(name)

if __name__ == '__main__':
    unittest.main()

In file subdir/file1.py:

class ClassA(unittest.TestCase):
    def test01(self):
        print self

In file subdir/file2.py:

class ClassB(unittest.TestCase):
    def test01(self):
        print self

Output when you run "runner.py":

C:\svn\stackoverflow>runner
test01 (__main__.ClassA)
.test01 (__main__.ClassB)
.
----------------------------------------------------------------------
Ran 2 tests in 0.004s

OK

Note that this is basically equivalent to textually including all the test files in the main file, similar to how #include "file.h" works in C. As you can see, the environment (namespace) for the sub-files is that of the file in which execfile() is called, which is why they don't even need to do their own "import unittest" calls. That should be okay if they never need to be run standalone, or you could just include the usual unittest boilerplate in each file if they do need to work on their own. You couldn't have duplicate class names anywhere in the included files, and you may notice other difficulties.

I'm pretty sure, however, that you'd be better off using something like nose or py.test which do this sort of "test collection" much better than unittest.

Peter Hansen
A: 
## This will execute the tests we need to run

import sys, glob, os, time

def run_all_tests():
    sys.path.append('/com/inthinc/python/tiwiPro/usedbyall/run files')
    run_all_tests = glob.glob('/com/inthinc/python/tiwipro/usedbyall/run files/*.py')

    for i in range( len(run_all_tests) ):
        replaceme = run_all_tests[i]
  withoutpy = replaceme.replace( '.py', '')
  withouttree = withoutpy.replace( '/com/inthinc/python/tiwipro/usedbyall/run files\\', '' )
  exec("import " + withouttree)
  exec( withouttree + ".run_test()" )
 if __name__ == '__main__':
  os.system( "taskkill /im java.exe" )

if __name__ == '__main__':
 os.startfile( "C:/com/inthinc/python/tiwiPro/usedbyall/start_selenium.bat" )
 time.sleep( 10 )
 run_all_tests()

This is what I ended up using. I simply added the run_test() method to each test so I can call them externally like a regular method. This works perfectly, and gives me more control of the test. Also I added a short line that will open the selenium RC server, and close it afterwards.

David Tanner