Unit testing framework: Run tests in new processes
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?
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.
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.
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).
精彩评论