Ho usato solo unittest per un breve periodo. Sto usando Jython 2.7.10 "versione finale"Come si usa unittest.TestResult?
Negli Python 2.7 documentazione che spiega TestResult dice:
I seguenti metodi della classe TestResult sono utilizzati per mantenere i strutture dati interne, e possono essere esteso in sottoclassi per supportare requisiti di report aggiuntivi. Ciò è particolarmente utile negli strumenti di costruzione che supportano la segnalazione interattiva mentre i test sono eseguiti .
StartTest (test) ... stopTest (test) ... startTestRun() ... stopTestRun() ¶
Questo è quello che voglio fare ... ma non riesco a capire come usi TestResult. Ecco uno SSCCE ...
import unittest
class TestResultX(unittest.TestResult):
def startTest(self, test):
print('# blip')
unittest.TestResult.startTest(self, test)
def stopTest(self, test):
print('# blop')
unittest.TestResult.stopTest(self, test)
def startTestRun(self):
print('# blep')
unittest.TestResult.startTestRun(self)
def stopTestRun(self):
print('# blap')
unittest.TestResult.stopTestRun(self)
class TestCaseX(unittest.TestCase):
def test_nonsense(self):
print('# wotcha')
self.assertTrue(False)
def run(self, test_result=None):
print('# spoons starting...')
test_result = TestResultX()
unittest.TestCase.run(self, test_result)
print('# ...spoons ended, tr %s' % (test_result, ))
unittest.main()
risultati in:
# spoons starting...
----------------------------------------------------------------------
Ran 0 tests in 0.015s
OK
# blip
# wotcha
# blop
# ...spoons ended, tr <__main__.TestResultX run=1 errors=0 failures=1>
Domande:
- Perché si dicono
0 tests
? - Perché
blep
eblap
(inizio e fine corsa) non vengono stampati?
Su una nota più generale:
Qualcuno può forse puntare a un buon tutorial/libro che spiega "uso corretto"/"buona pratica" quando si tratta di TestResult, TestRunner, TestLoader, ecc Ho ottenuto "TDD con Python", ma non sembra spiegare nulla di tutto questo.
Qualcuno potrebbe dirmi perché unittest2 sembra essere usato al posto di unittest?
addendum
Dopo gli sforzi di Omar Diab a guardare il codice sorgente Ho provato questo:
def run(self, *args, **kvargs):
result = self.defaultTestResult()
startTestRun = getattr(result, 'startTestRun', None)
logger.info('# calling superclass run... startTestRun? %s' % (startTestRun,))
unittest.TestCase.run(self, *args, **kvargs )
logger.info('# ... superclass run ended')
Purtroppo ogni metodo test_XXX poi ha dato:
# calling superclass run... startTestRun? <bound method TestResult.startTestRun of <unittest.result.TestResult run=0 errors=0 failures=0>>
setUp for test_that_stuff_happened (__main__.xx_FT)
tearDown for test_that_stuff_happened (__main__.xx_FT)
end tearDown...
. # ... superclass run ended
Mi sono imbattuto in questo - devo dire, la documentazione di tutto questo è piuttosto orribile ... –