Python中的Unittest可以运行由字符串组成的列表吗?

2024-06-16 08:46:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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"

Tags: py文件夹comtestsunittestglobreplacereplaceme
2条回答

虽然我不推荐这种方法(也不推荐您尝试做什么),但这里有一个简单的方法,它可以大致完成您想要的。在

在文件中“跑步者.py“(例如,与上述类似): 导入全局 导入单元测试

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

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

在subdir/file1.py文件中:

^{pr2}$

在subdir/file2.py文件中:

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

运行时输出“跑步者.py“:

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

OK

请注意,这基本上等同于在主文件中以文本方式包含所有测试文件,类似于C中#include“file.h”的工作方式。正如您所见,子文件的环境(命名空间)是调用execfile()的文件的环境(命名空间),这就是它们甚至不需要执行自己的“import unittest”调用的原因。如果它们不需要独立运行,那么这应该是可以的,或者如果它们确实需要独立运行,那么可以在每个文件中包含通常的unittest样板。你不能在包含的文件中有重复的类名,你可能会注意到其他的困难。在

不过,我很确定,您最好使用nose或{a2}之类的东西,它们比unittest做这种“测试集合”要好得多。在

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

相关问题 更多 >