2012-08-17 12 views
21

Ho due casi di test (due file diversi) che voglio eseguire insieme in una Test Suite. Posso eseguire i test semplicemente eseguendo python "normalmente" ma quando seleziono per eseguire un test di python-unit si dice che eseguono 0 test. In questo momento sto solo cercando di ottenere almeno un test da eseguire correttamente.Provare ad implementare Python TestSuite

import usertest 
import configtest # first test 
import unittest # second test 

testSuite = unittest.TestSuite() 
testResult = unittest.TestResult() 
confTest = configtest.ConfigTestCase() 
testSuite.addTest(configtest.suite()) 
test = testSuite.run(testResult) 
print testResult.testsRun # prints 1 if run "normally" 

Ecco un esempio del mio banco di prova istituito

class ConfigTestCase(unittest.TestCase): 
    def setUp(self): 

     ##set up code 

    def runTest(self): 

     #runs test 


def suite(): 
    """ 
     Gather all the tests from this module in a test suite. 
    """ 
    test_suite = unittest.TestSuite() 
    test_suite.addTest(unittest.makeSuite(ConfigTestCase)) 
    return test_suite 

if __name__ == "__main__": 
    #So you can run tests from this module individually. 
    unittest.main() 

Che cosa devo fare per ottenere questo lavoro in modo corretto?

risposta

38

si desidera utilizzare un testuit. Quindi non è necessario chiamare unittest.main(). L'utilizzo di testsuit dovrebbe essere così ..

#import usertest 
#import configtest # first test 
import unittest # second test 

class ConfigTestCase(unittest.TestCase): 
    def setUp(self): 
    print 'stp' 
     ##set up code 

    def runTest(self): 

     #runs test 
    print 'stp' 

def suite(): 
    """ 
     Gather all the tests from this module in a test suite. 
    """ 
    test_suite = unittest.TestSuite() 
    test_suite.addTest(unittest.makeSuite(ConfigTestCase)) 
    return test_suite 

mySuit=suite() 


runner=unittest.TextTestRunner() 
runner.run(mySuit) 
+0

basta eseguire il codice –

+0

Grazie, grazie ha funzionato perfettamente per quello che mi serviva. Grazie mille! – avoliva

+2

Grazie mille per questa risposta. Perché mai dovrei chiamare 'unittest.makeSuite' per aggiungere un test a una * esistente * suite? – timgeb

4

Tutto il codice per creare un caricatore e una suite non è necessario. Dovresti scrivere i tuoi test in modo che siano eseguibili tramite il test discovery usando il tuo runner preferito. Significa semplicemente denominare i metodi in un modo standard, inserirli in un posto che può essere importato (o passare una cartella che li contiene al corridore) e ereditare da unittest.TestCase. Dopo averlo fatto, puoi usare python -m unittest discover al più semplice, o un più bel corridore di terze parti per scoprire e quindi eseguire i test.

0

Io parto dal presupposto che si fa riferimento a eseguire test di python-unit contro il modulo che consolida i due test. Funzionerà se crei un test case per quel modulo, ad es. sottoclasse unittest.TestCase e con un semplice test che inizia con la parola 'test'.

ad es.

class testall(unittest.TestCase): 

    def test_all(self):   
     testSuite = unittest.TestSuite() 
     testResult = unittest.TestResult() 
     confTest = configtest.ConfigTestCase() 
     testSuite.addTest(configtest.suite()) 
     test = testSuite.run(testResult) 
     print testResult.testsRun # prints 1 if run "normally" 

if __name__ == "__main__": 
     unittest.main() 
1

Se si sta tentando di raccogliere manualmente TestCase s, questo è utile: unittest.loader.findTestCases():

# Given a module, M, with tests: 
mySuite = unittest.loader.findTestCases(M) 
runner = unittest.TextTestRunner() 
runner.run(mySuit) 
Problemi correlati