[Pyunit-interest] How do you import a module with unit tests and then run them?
Brought to you by:
purcell
From: Andrew P. L. <bs...@ma...> - 2002-08-01 00:31:54
|
I'm guessing that I'm doing something obviously wrong, but darned if I can figure it out. I have a program called test_unittest.py (attached). It runs; it passes. Life is good. So, now I have a program called test_unittest2.py (attached) which I want to import test_unittest.py and run the unit tests. What I wind up with: <Big traceback omitted> File "/usr/local/lib/python2.2/unittest.py", line 451, in loadTestsFromName obj = getattr(obj, part) AttributeError: 'module' object has no attribute 'suite' Clearly I've got some locals/globals namespace issue, but I don't know how to fix it. So, how do I get test_unittest2.py to call the tests in test_unittest.py and run them correctly? I am open to changing the original test code, explicitly assigning stuff, etc. to make this work. I would even use execfile or the imp module if required. Lest someone think that this is just a really stupid thing to do, I will point out that the same issues arise if you try to use the python debugger on a unittest as it also wants to import the module and then run the unit tests. Thanks, -a <Begin test_unittest.py> #!/usr/bin/env python import unittest class MyTest(unittest.TestCase): def checkBasic(self): assert 1 == 1 def suite(): """Suite of testcases""" suitelist = [] suitelist.append(unittest.makeSuite(MyTest, 'check')) return unittest.TestSuite(suitelist) def main(): unittest.main(defaultTest="suite") if __name__ == "__main__": main() <End test_unittest.py> <Begin test_unittest2.py> #!/usr/bin/env python import test_unittest def main(): test_unittest.main() if __name__ == "__main__": main() <End test_unittest2.py> |