Python unittest discover没有检测到所有te

2024-03-28 16:23:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个叫做scratch的目录中有3个测试

Scenario
.
├── __init__.py
├── loadtest.py
├── TestIsNumeric.py
└── TestLoad.py

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

^{pr2}$

在负载测试.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

在测试Meric.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')

在测试负载.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()

使用鼻子测试——vv——只收集

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

Tags: namepytestimportselfdatadefsetup