bdb.Bdb (Debugger Base Class) / unittest Interaction

Ami Tavory atavory at gmail.com
Sun Mar 25 16:42:52 EDT 2012


  Hello,

  I'm having some difficulties with the interaction between bdb.Bdb and
scripts which contain unittest. Following are two simplified scenarios of a
GUI debugger Gedit plugin I'm writing based on bdb.Bdb, and a script that
is being debugged by it.

--Scenario A--

  The script being debugged is foo.py; its content is
<code>
print 'Hello, world!'
</code>

  The "debugger" (a simplified version of it) is
<code>
import bdb

g = {}
g['__name__'] = '__main__'
g['__file__'] = 'foo.py'
statement = 'execfile("%s", %s)' % ('foo.py', str(g))
bdb.Bdb().run(statement)
</code>

it indeed prints 'Hello, world'.

--Scenario B--

  The script being debugged is bar.py; its content is
<code>
import unittest

class test(unittest.TestCase):
    def test(self):
        print 'Hello, world!'

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(test)

if __name__ == '__main__':
    unittest.main()
</code>

  The "debugger" is identical to before, but with 'foo.py' replaced by
'bar.py'. It does not print 'Hello, world'. In fact, it prints
<output>
----------------------------------------------------------------------
Ran 0 tests in 0.000s
</output>

 However:

   1. Running bar.py as a script, indeed prints 'Hello, world'.
   2. Running pdb (which internally uses bdb.Bdb) on bar.py, also indeed
   prints 'Hello, world'.

  I've looked at the code of pdb to see what I'm doing wrong (at least in
the second case), but couldn't find the difference. Help would be much
appreciated.


  Thanks & Bye,


  Ami
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120325/af54d65c/attachment.html>


More information about the Python-list mailing list