[英]Python unittest discover does not detects all the test
我在名為草稿的目錄中進行了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()
使用鼻子測試--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
在測試類的函數前面添加test_。 對於實例def test_load_tests
然后從命令行運行程序即可!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.