单元测试框架:在新流程中运行测试
我编写了一个使用 单元测试框架 的 GUI 应用程序。 该应用程序在树视图中显示所有找到的单元测试。 如果单击单元测试名称,程序将运行该测试并显示结果。 有用!
但现在我希望每个测试用例都在一个新进程中运行。 我创建了一个“工作线程”(使用模块线程),但该线程也在同一进程中运行。
“多处理”模块是解决方案,但我的实现遇到了问题。 我将“测试执行代码”放在自己的函数中并尝试:
item = self.GetSelection()
name = self.GetItemText(item)
p = multiprocessing.Process(target=ExecuteTest, args=(name, item))
p.daemon = True
p.start()
但后来出现了错误。
Traceback (most recent call last):
File "C:\a\b\c\hhh\a.py", line 577, in OnLeftDClick
p.start()
File "C:\Python26\lib\multiprocessing\process.py", line 104, in start
self._popen = Popen(self)
File "C:\Python26\lib\multiprocessing\forking.py", line 239, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:\Python26\lib\multiprocessing\forking.py", line 162, in dump
ForkingPickler(file, protocol).dump(obj)
File "C:\Python26\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Python26\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python26\lib\pickle.py", line 419, in save_reduce
save(state)
File "C:\Python26\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python26\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Python26\lib\pickle.py", line 681, in _batch_setitems
save(v)
File "C:\Python26\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python26\lib\pickle.py", line 548, in save_tuple
save(element)
File "C:\Python26\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python26\lib\pickle.py", line 419, in save_reduce
save(state)
File "C:\Python26\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python26\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Python26\lib\pickle.py", line 686, in _batch_setitems
save(v)
File "C:\Python26\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python26\lib\pickle.py", line 396, in save_reduce
save(cls)
File "C:\Python26\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python26\lib\pickle.py", line 748, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <type 'PySwigObject'>: it's not found as __builtin__.PySwigObject
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python26\lib\multiprocessing\forking.py", line 342, in main
self = load(from_parent)
File "C:\Python26\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python26\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python26\lib\pickle.py", line 880, in load_eof
raise EOFError
EOFError
有人有什么想法吗?
I programmed a GUI application who use the Unit testing framework.
The application shows all found unit tests in a tree-view.
If you click on a unit-test name, the program will run this test and show the result.
It works!
But now I want that every test case running in a new process.
I created a "Working Thread" (with the module threading) but this thread is also running in the same process.
The module "multiprocessing" is the solution but I got problems with my implementation.
I put my "test execute code" in a own function and tried:
item = self.GetSelection()
name = self.GetItemText(item)
p = multiprocessing.Process(target=ExecuteTest, args=(name, item))
p.daemon = True
p.start()
But then I got errors.
Traceback (most recent call last):
File "C:\a\b\c\hhh\a.py", line 577, in OnLeftDClick
p.start()
File "C:\Python26\lib\multiprocessing\process.py", line 104, in start
self._popen = Popen(self)
File "C:\Python26\lib\multiprocessing\forking.py", line 239, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:\Python26\lib\multiprocessing\forking.py", line 162, in dump
ForkingPickler(file, protocol).dump(obj)
File "C:\Python26\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Python26\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python26\lib\pickle.py", line 419, in save_reduce
save(state)
File "C:\Python26\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python26\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Python26\lib\pickle.py", line 681, in _batch_setitems
save(v)
File "C:\Python26\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python26\lib\pickle.py", line 548, in save_tuple
save(element)
File "C:\Python26\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python26\lib\pickle.py", line 419, in save_reduce
save(state)
File "C:\Python26\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python26\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Python26\lib\pickle.py", line 686, in _batch_setitems
save(v)
File "C:\Python26\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python26\lib\pickle.py", line 396, in save_reduce
save(cls)
File "C:\Python26\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python26\lib\pickle.py", line 748, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <type 'PySwigObject'>: it's not found as __builtin__.PySwigObject
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python26\lib\multiprocessing\forking.py", line 342, in main
self = load(from_parent)
File "C:\Python26\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python26\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python26\lib\pickle.py", line 880, in load_eof
raise EOFError
EOFError
Does anybody have any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,你试图“pickle”一些不可pick的东西(似乎是一个wx对象)。尝试仅使用“简单”对象(即内部没有图形对象)作为参数,它应该工作得更好。
您还可以查看 python nose unitest 库,它允许您自动在多进程中运行测试。
Obviously, you're trying to "pickle" something that is not pickable (seems to be a wx object). Try to use as parameter only "simple" objects (ie without graphical object inside), and it should be working better.
You could also take a look to the python nose unitest library which allows you to run tests in multiprocesses automatically.
我建议您创建一个运行测试的单独脚本,然后您可以使用 subprocess 模块调用该脚本,并为其指定要运行的测试的名称。
好处是,您可以在持续集成服务器、夜间构建或其他不需要 GUI 的批处理类型过程中使用相同的脚本。
I recommend you create a separate script that runs a test, then you can use the
subprocess
module to call that script, giving it the names of tests to run.The bonus is, you can then use that same script in a continuous integration server, nightly build, or other batch-type processes that don't require a GUI.
使用 fork 生成子进程。这需要您做一些工作,但会起作用。
请注意:multiprocessnig 模块是使用 fork 实现的。
更新: 仔细查看代码后,似乎
item
viariable 不可 pickle (这是一个词吗?)。我认为你不需要它,因为你可以用更简单的东西(一个值或某种)替换它。Spawn a child process using fork. This will require some work from you, but will work.
As a note: the multiprocessnig module is implemented using fork.
Update: After looking closely at your code it seems that the
item
viariable is not pickleable (is that a word?). I don't think you need it, as you could replace it with something simpler (a value or some sort).