Python 中的 Unittest 可以运行由字符串组成的列表吗?
我正在使用 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"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我不会完全推荐这种方法(也可能不是您想要做的),但这里有一个简单的方法,似乎可以大致完成您想要的事情。
在文件“runner.py”中(例如,与上面类似):
导入全局
import unittest
In file subdir/file1.py:
In file subdir/file2.py:
运行“runner.py”时的输出:
请注意,这基本上相当于在主文件中以文本方式包含所有测试文件,类似于 # include "file.h" 在 C 中工作。如您所见,子文件的环境(命名空间)是调用 execfile() 的文件的环境(命名空间),这就是为什么它们甚至不需要执行其操作自己的“导入单元测试”调用。如果它们不需要独立运行,那应该没问题,或者如果它们确实需要独立运行,您可以在每个文件中包含常用的单元测试样板。在包含的文件中的任何地方都不能有重复的类名,并且您可能会注意到其他困难。
不过,我很确定您最好使用 nose 或 py.test 比 unittest 做这种“测试集合”要好得多。
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
In file subdir/file1.py:
In file subdir/file2.py:
Output when you run "runner.py":
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.
这就是我最终使用的。我只是将 run_test() 方法添加到每个测试中,这样我就可以像常规方法一样从外部调用它们。这非常有效,让我可以更好地控制测试。我还添加了一条短线,用于打开 selenium RC 服务器,然后关闭它。
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.