在多个文件夹中使用测试用例进行简单的Python单元测试

在多个文件夹中使用测试用例进行简单的Python单元测试,python,python-unittest,Python,Python Unittest,我是使用python进行单元测试的新手,正在设计一个简单的单元测试设置。这就是我所做的: /python_test __init__.py unittest_main.py test_abc/ __init__.py test_abc.py test_pqr/ __init__.py test_pqr.py 所有的_init__u;.py文件都是空的,其他文件都有一些基本内容: unitt

我是使用python进行单元测试的新手,正在设计一个简单的单元测试设置。这就是我所做的:

/python_test
    __init__.py
    unittest_main.py
    test_abc/
        __init__.py
        test_abc.py    
    test_pqr/
        __init__.py
        test_pqr.py 
所有的_init__u;.py文件都是空的,其他文件都有一些基本内容:

unittest\u 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"
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()
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()
测试abc.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"
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()
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()
测试\u pqr.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"
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()
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()
问题:

我可以单独测试测试用例,但是当我直接使用父文件来运行所有测试时,什么都没有发生

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

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

OK

安装的Python版本是2.7.3

因为您要将模块名称传递给加载程序,所以您希望使用
loadTestsFromName
而不是
loadTestsFromModule

谢谢@ddelemeny!是的,解决了。我还将文件夹名作为模块名的一部分传递,我也删除了模块名。这使得它能够工作:suiteList.append(unittest.defaultTestLoader.loadTestsFromName(testTarget+“+testTarget))