0

我正在使用 Selenium 在网站上运行测试。我需要运行许多单独的测试,并且想要创建一个脚本来运行某个文件夹中的所有 python 文件。我可以获取名称并导入模块,但是一旦这样做,我就无法让 unittest 运行文件。这是我创建的一些测试代码。我的问题似乎是,一旦我将名称作为字符串输入,我就无法摆脱它。

我想为每个文件夹编写其中一个文件,或者以某种方式执行目录中的所有文件夹。这是我到目前为止的代码:

\## 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"
4

2 回答 2

0
## 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 服务器,然后关闭它。

于 2009-12-04T17:28:11.463 回答
0

虽然我不会完全推荐这种方法(也可能是你想要做的),但这里有一个简单的方法,似乎可以大致完成你想要的。

在文件“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”调用。如果它们永远不需要独立运行,那应该没问题,或者如果它们确实需要自己工作,您可以在每个文件中包含通常的单元测试样板。您不能在包含的文件中的任何地方有重复的类名,并且您可能会注意到其他困难。

但是,我很确定,您最好使用类似nosepy.test的东西,它们比unittest 做得更好。

于 2009-12-04T03:39:54.983 回答