gpt4 book ai didi

Pythonunittestdiscover没有检测到所有测试

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:23 24 4
gpt4 key购买 nike

我在名为 scrap 的目录中有 3 个测试

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

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

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

OK

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

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

最佳答案

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

然后从命令行运行你的程序,它就会工作!

关于Pythonunittestdiscover没有检测到所有测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32891493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com