2016-01-12 17 views
13

Utilizzo la struttura pythonunittest. È possibile specificare per le abilità del framework un timeout per il test? Se no, è possibile specificare con garbo un timeout per tutti i test e per alcuni test separati un valore privato per ognuno?
Voglio definire un global timeout per tutti i test (lo useranno per impostazione predefinita) e un timeout per alcuni test che possono richiedere molto tempo.Come specificare il timeout del test per Python Unittest?

risposta

13

Per quanto ne so, lo unittest non contiene alcun supporto per il timeout dei test.

Puoi provare la libreria timeout-decorator da PyPI. Applicare il decoratore sui singoli test per farli terminare se prendono troppo tempo:

import timeout_decorator 

class TestCaseWithTimeouts(unittest.TestCase): 

    # ... whatever ... 

    @timeout_decorator.timeout(LOCAL_TIMEOUT) 
    def test_that_can_take_too_long(self): 
     sleep(float('inf')) 

    # ... whatever else ... 

Per creare un timeout globale, è possibile sostituire chiamare

unittest.main() 

con

timeout_decorator.timeout(GLOBAL_TIMEOUT)(unittest.main)() 
+0

Interessante. Non sto usando 'unittest.main()', ma spero di poter adottare 'decorator' per il mio caso. Ma i miei test non stanno andando in single thread ... – Jury

+0

@Jury Controlla la sezione "Multithreading" in ['timeout-decorator' reference] (https://pypi.python.org/pypi/timeout-decorator) - hai solo è necessario utilizzare 'timeout_decorator.timeout (TIMEOUT, use_signals = False)' in ambiente multi-thread. – Lav

+0

Sì, l'ho visto. Ci proverò. – Jury

Problemi correlati