2010-08-23 76 views
2

我想要一个单独的类来运行我所有的测试,然后从main调用该类来显示结果。不幸的是,我得到这样的错误:使用python的unittest模块的未绑定错误

Traceback (most recent call last): 
    File "/home/dhatt/workspace/pyqt_DALA_ServiceTracker/src/Main.py", line 21, in <module> 
    allsuite = unittest.TestLoader.loadTestsFromModule(TestAllSuite) 
TypeError: unbound method loadTestsFromModule() must be called with TestLoader instance as first argument (got type instance instead) 

如何更改我的代码,以便我可以运行我的测试?

我正在使用python 2.6.5。

如果您发现我的测试结构本身也存在问题(主调用主测试类,然后调用所有其他测试类),请告诉我。我愿意接受建议,以我的测试代码更好的布局(在互联网上传播的例子只是使用单元测试时给你最基础的)

下面是从TestAllClass我的代码(这是调用其他的测试类)

import unittest 
from TestSettings import TestSettings 
from TestConnectDB import TestConnectDB 


class TestAllSuite(unittest.TestCase): 

    def testsuite(self): 
     suite_settings = TestSettings.suite() 
     suite_connectDB = TestConnectDB.suite() 
     alltests = unittest.TestSuite([suite_settings, suite_connectDB]) 
     return alltests 

,这里是我的主要的组成部分,调用TestAllClass

if __name__ == '__main__': 
    allsuite = unittest.TestLoader.loadTestsFromModule(TestAllSuite) 
    unittest.TextTestRunner(verbosity=2).run(allsuite) 

这里是我的测试类的一个实例(如果这能帮助):

from Settings import Settings 
import unittest 


class TestSettings(unittest.TestCase): 
    def suite(self): 
     suite = unittest.TestSuite() 
     tests = ['test_confirm_ip', 'test_valid_conn','test_correct_location'] 
     return unittest.TestSuite(map(suite, tests)) 

    def setUp(self): 
     self._test_settings = Settings('/home/dhatt/ServiceTrackerSettings.ini') 
     self.ip_add, self.conn, self.digby = self._test_settings._get_config_variables() 

    def tearDown(self): 
     self._test_settings = None 
     self.ip_add = None 
     self.conn = None 
     self.digby = None 

    def test_confirm_ip(self): 
     self.assertEqual((str(self.ip_add)), ('142.176.195.250')) 

    def test _confirm_conn(self): 
     self.assertEqual((str(self.conn)), ('conn1')) 

    def test_confirm_location(self): 
     self.assertEqual((self.digby), (True)) 

回答

4

的参数loadTestsFromModule(你的情况TestAllSuite),应该是一个模块,而不是unittest.TestCase一个子类:

allsuite = unittest.TestLoader.loadTestsFromModule(TestAllSuite) 

例如,这里是一个运行在文件中找到的所有单元测试的小脚本表格test_*.py

import unittest 
import sys 
import os 

__usage__=''' 
%prog  # Searches CWD 
%prog DIR 
''' 

if __name__=='__main__': 
    if len(sys.argv)>1: 
     unit_dir=sys.argv[1] 
    else: 
     unit_dir='.' 
    test_modules=[filename.replace('.py','') for filename in os.listdir(unit_dir) 
        if filename.endswith('.py') and filename.startswith('test_')] 
    map(__import__,test_modules) 

    suite = unittest.TestSuite() 
    for mod in [sys.modules[modname] for modname in test_modules]: 
     suite.addTest(unittest.TestLoader().loadTestsFromModule(mod)) 
    unittest.TextTestRunner(verbosity=2).run(suite) 
+0

好的。感谢您的澄清。感谢剧本,我从来没有想过要这样看。稍后我会看到它如何在添加更多测试的情况下执行,但目前看起来不错。 – 2010-08-24 12:12:36

+0

另外,如果我有2.7版本的Python,我可以做到这一点。 http://docs.python.org/library/unittest.html#unittest.TestLoader.discover 可惜... – 2010-08-24 14:43:44

+0

@丹尼:感谢您指出这一点。我不知道“TestLoader.discover”。 – unutbu 2010-08-24 14:50:14

相关问题