简体   繁体   中英

Unbound error using python's unittest module

I would like a seperate class to run all of my tests, and then call that class from main to display the results. Unfortunately, I am getting an error like this:

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)

How do I change my code so that I can run my tests?

I am using python 2.6.5.

If you find that my testing structure itself also poses a problem (where main calls the main testing class that then calls all my other testing classes), let me know. I am open to suggestions to a better layout of my testing code (the examples off of the internet only give you the very basics when using unittest)

Here is my code from TestAllClass (it is calling other test classes)

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

and here is part of my main that calls TestAllClass

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

Here is an example of one of my test classes (if that helps):

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))

The argument to loadTestsFromModule (in your case TestAllSuite ), should be a module, not a subclass of unittest.TestCase :

allsuite = unittest.TestLoader.loadTestsFromModule(TestAllSuite)

For example, here is a little script which runs all unit tests found in files of the form 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM