سؤال

I am new to unit testing using python and am designing a simple unit test setup. This is what I have made:

/python_test
    __init__.py
    unittest_main.py
    test_abc/
        __init__.py
        test_abc.py    
    test_pqr/
        __init__.py
        test_pqr.py 

All the __init__.py files are empty and the other files have some basic content:

unittest_main.py

import os
import sys
import glob
import inspect
import unittest
from sets import Set

if len(sys.argv) > 1:
    testCases = glob.glob('test_*')
    testTargets = Set([])
    for testTarget in sys.argv:
        if testTarget == "test_all":
            testTargets = testCases
            break
        else:
            for testCase in testCases:
                if testCase == testTarget:
                    testTargets.add(testCase)
                    break
    if len(testTargets) > 0:
        print testTargets
        suiteList = []
        for testTarget in testTargets:
            cmd_subfolder =    os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],testTarget)))
            if cmd_subfolder not in sys.path:
                sys.path.insert(0, cmd_subfolder)
            suiteList.append(unittest.defaultTestLoader.loadTestsFromModule("python_test"+"."+testTarget+"."+testTarget))
        mainSuite = unittest.TestSuite(suiteList)
        unittest.TextTestRunner(verbosity=1).run(mainSuite)
    else:
        "No Proper Test Targets Specified"
else:
    print "No Test Targets Specified"

test_abc.py

import unittest

class test_abc(unittest.TestCase):

    def setUp(self):
        print "Setup Complete"

    def tearDown(self):
        print "Tear Down Complete"

    def test_abc_main(self):
        self.assertEqual(1, 2, "test-abc Failure")

if __name__ == "__main__":
    unittest.main()

test_pqr.py

import unittest

class test_pqr(unittest.TestCase):

    def setUp(self):
        print "Setup Complete"

    def tearDown(self):
        print "Tear Down Complete"

    def test_abc_main(self):
        self.assertEqual(1, 2, "test-pqr Failure")

if __name__ == "__main__":
    unittest.main()

Question:

I am able to test the test cases separately but when I use the parent directly file to run all the tests, nothing happens?

$ ~/src/python_test$ python unittest_main.py test_all
['test_pqr', 'test_abc']

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

The installed Python Version is 2.7.3

هل كانت مفيدة؟

المحلول

Because you're passing module names to the loader, you want to use loadTestsFromName instead of loadTestsFromModule.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top