Python unittest discover不会检测所有测试

时间:2015-10-01 15:33:08

标签: python python-unittest

我在名为scratch

的目录中有3个测试
Scenario
.
├── __init__.py
├── loadtest.py
├── TestIsNumeric.py
└── TestLoad.py

现在,当我尝试python -m unittest发现它没有检测到任何测试

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

OK

loadtest.py

import unittest

class GeneralTestCase(unittest.TestCase):
    def __init(self, methodName, param1, param2):
        super(GeneralTestCase, self).__init__(methodName)

        self.param1 = param1
        self.param2 = param2

    def runTest(self):
        pass  # Test that depends on param 1 and 2.


def load_tests(loader, tests, pattern):
    test_cases = unittest.TestSuite()
    for p1, p2 in [(1, 2), (3, 4)]:
        test_cases.addTest(GeneralTestCase('runTest', p1, p2))
    return test_cases

testisnumeric.py

import unittest
#from testscenarios import TestWithScenarios
import testscenarios

scenario1 = ('basic', {'attribute': 'value'})
#scenario2 = ('advanced', {'attribute': 'value2'})

class TestPython(testscenarios.TestWithScenarios):
    scenarios = [('',dict(name='temp')),
                    ('Scenario-2',dict(name='temp')),
                    ('Scenario-3',dict(name='temp'))]


    def test_method(self):
        self.assertEqual(self.name,'temp')

testload.py

import unittest

def load_tests(loader, tests, pattern):
    print 'load_tests called'
    f = ['a','b']  # data.csv contains three lines: "a\nb\nc"
    tests = unittest.TestSuite()
    for line in f:
        tc = Foo()
        tc.setup(line)
        tests.addTest(tc)
    return tests

class Foo(unittest.TestCase):
    def setup(self,bar):
        print "Foo.setup()"
        #print dir(self)
        self.bar = bar


    def runTest(self):
        print 'running'
        print self.bar

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

使用nosetests --vv --collect-only

nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
nose.selector: INFO: /data/scratch/scenario/loadtest.py is executable; skipped
nose.selector: INFO: /data/scratch/scenario/TestIsNumeric.py is executable; skipped
nose.selector: INFO: /data/scratch/scenario/TestLoad.py is executable; skipped

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

OK

1 个答案:

答案 0 :(得分:1)

在测试类中添加test_在函数前面。 例如def test_load_tests

然后从命令行运行你的程序吧!