我正在使用Selenium在一个网站上运行测试。我有许多单独的测试,我需要运行,并希望创建一个脚本,将运行在某个文件夹中的所有python文件。我可以获取名称并导入模块,但一旦这样做,就无法让单元测试运行这些文件。下面是我创建的一些测试代码。我的问题似乎是,一旦我将名字作为字符串输入,我就无法摆脱它。
我想为每个文件夹编写一个这样的文件,或者以某种方式执行一个目录中的所有文件夹。以下是我到目前为止拥有的代码:
\## 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"发布于 2009-12-04 11:39:55
尽管我并不完全推荐这种方法(也不一定是您想要做的事情),但这里有一种简单的方法,它似乎可以大致实现您想要的结果。
在文件"runner.py“中(例如,类似于上面的):import glob import unittest
testfiles = glob.glob('subdir/*.py')
for name in testfiles:
execfile(name)
if __name__ == '__main__':
unittest.main()在文件subdir/file1.py中:
class ClassA(unittest.TestCase):
def test01(self):
print self在文件subdir/file2.py中:
class ClassB(unittest.TestCase):
def test01(self):
print self运行“runner.py”时的输出:
C:\svn\stackoverflow>runner
test01 (__main__.ClassA)
.test01 (__main__.ClassB)
.
----------------------------------------------------------------------
Ran 2 tests in 0.004s
OK注意,这基本上等同于在文本上包含主文件中的所有测试文件,类似于#include "file.h“在C中的工作方式。正如您所看到的,子文件的环境(命名空间)是调用execfile()的文件的环境,这就是为什么它们甚至不需要执行自己的"import unittest”调用。如果它们永远不需要独立运行,这应该是可以的,或者如果它们确实需要独立工作,那么您可以在每个文件中只包含常用的单元测试样板。在所包含的文件中不能有重复的类名,而且您可能会注意到其他困难。
然而,我非常确定,你最好使用像nose或py.test这样的东西,它们比unittest做这种“测试收集”要好得多。
发布于 2009-12-05 01:28:11
## 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()这就是我最终使用的。我只是将run_test()方法添加到每个测试中,以便可以像常规方法一样在外部调用它们。这可以完美地工作,让我可以更好地控制测试。我还添加了一行简短的代码,用于打开selenium RC服务器,然后将其关闭。
https://stackoverflow.com/questions/1446317
复制相似问题