Я пытаюсь создать собственный отчет о выполнении тестового набора pyunit, но попал в сообщение с ошибкой «no attributue».Создание пользовательского отчета pyunit
import json
import unittest
import sys
class MyTestResult(unittest._TextTestResult):
def addSuccess(self, test):
TestResult.addSuccess(self, test)
def addError(self, test, err):
TestResult.addError(self, test, err)
def addFailure(self, test, err):
TestResult.addFailure(self, test, err)
class MyTestRunner(unittest.TextTestRunner):
def _makeResult(self, verbosity):
return MyTestResult(self.stream, self.descriptions, verbosity)
class TestServer(unittest.TestCase):
def testFunction1(self):
res = True
self.assertTrue(res, "test case failed")
def testFunction2(self):
res = 5
self.assertEqual(res, 5)
def testFunction3(self):
res = True
self.assertEqual(res, True, 'test case failed')
def testFunction4(self):
res = False
self.assertEqual(res, True, 'test case failed')
# Create an instance of each test case.
testCase1 = TestServer('testFunction1')
testCase2 = TestServer('testFunction2')
testCase3 = TestServer('testFunction3')
testCase4 = TestServer('testFunction4')
# Add test cases to the test suite.
testSuite = unittest.TestSuite()
testSuite.addTest(testCase1)
testSuite.addTest(testCase2)
testSuite.addTest(testCase3)
testSuite.addTest(testCase4)
# Execute the test suite.
testRunner = unittest.MyTestRunner(verbosity=2)
testRunner.run(testSuite)
Ошибка, которую я получаю ниже. Мне также нужна помощь в настройке моего итогового отчета об испытаниях, чтобы я мог добавить дополнительную информацию, чем генерирует один пинит. Что я должен реализовать в классе «MyTestResult»?
bash-3.2$ python myreport.py
Traceback (most recent call last):
File "myreport.py", line 45, in <module>
testRunner = unittest.MyTestRunner(verbosity=2)
AttributeError: 'module' object has no attribute 'MyTestRunner'
Кроме того, я искал предложение изменить тестовый отчет, который по умолчанию будет указан ниже.
bash-3.2$ python myreport.py
testFunction1 (__main__.TestServer) ... ERROR
testFunction2 (__main__.TestServer) ... ok
testFunction3 (__main__.TestServer) ... ok
testFunction4 (__main__.TestServer) ... FAIL
Используя код, как есть, я получаю следующее сообщение об ошибке: Файл "myreport.py", строка 49, в testRunner.run (Тестов) Файл «/ пользователей/anjangam/pyvenv/venv/lib/python2.7/site-packages/unittest.py ", строка 330, в __call__ тест (результат) Файл" /users/anjangam/pyvenv/venv/lib/python2.7/site -packages/unittest.py ", строка 209, в __call__ result.addError (self, self .__ exc_info()) Файл« myreport.py », строка 10, в addError super (MyTestResult, self) .addError (test , err) TypeError: должен быть тип, а не classobj –
AnilJ
@AnilJ, Check this out: http://ideone.com/AF4n6w – falsetru
Использование этой строки 'unittest._TextTestResult.addFailure (self, test, err)' решает проблему. – AnilJ