2011-01-12 28 views
29

vorrei mettere alcune dichiarazioni di registrazione all'interno di funzione di test per esaminare alcune variabili di stato.registrazione all'interno di test py.test

ho il seguente frammento di codice:

import pytest,os 
import logging 

logging.basicConfig(level=logging.DEBUG) 
mylogger = logging.getLogger() 

############################################################################# 

def setup_module(module): 
    ''' Setup for the entire module ''' 
    mylogger.info('Inside Setup') 
    # Do the actual setup stuff here 
    pass 

def setup_function(func): 
    ''' Setup for test functions ''' 
    if func == test_one: 
     mylogger.info(' Hurray !!') 

def test_one(): 
    ''' Test One ''' 
    mylogger.info('Inside Test 1') 
    #assert 0 == 1 
    pass 

def test_two(): 
    ''' Test Two ''' 
    mylogger.info('Inside Test 2') 
    pass 

if __name__ == '__main__': 
    mylogger.info(' About to start the tests ') 

    pytest.main(args=[os.path.abspath(__file__)]) 

    mylogger.info(' Done executing the tests ') 

ottengo il seguente output:

[bmaryada-mbp:/Users/bmaryada/dev/platform/main/proto/tests/tpch $]python minitest.py 
INFO:root: About to start the tests 
======================================================== test session starts ========================================================= 
platform darwin -- Python 2.6.2 -- pytest-2.0.0 
collected 2 items 

minitest.py .. 

====================================================== 2 passed in 0.01 seconds ====================================================== 
INFO:root: Done executing the tests 

Si noti che solo i messaggi di registrazione dal blocco '__name__ == __main__' vengono trasmessi alla console.

C'è un modo per forzare pytest per emettere la registrazione per consolare con metodi di prova come bene?

+2

Si potrebbe dare un'occhiata a [questa risposta] (http://stackoverflow.com/a/11877951/148680), scritto dal creatore di py.test. Suggerisce un plugin pytest che offre un alto grado di versatilità. – chb

risposta

17

funziona per me, ecco l'output ottengo: [omissis -> esempio non è corretto]

Edit: Sembra che si deve passare l'opzione -s per py.test in modo da non catturare stdout. Qui (py.test non installato), è stato sufficiente utilizzare python pytest.py -s pyt.py.

per il codice, tutto ciò che serve è quello di passare -s in args-main:

pytest.main(args=['-s', os.path.abspath(__file__)]) 

Vedere la documentazione py.test su capturing output.

+0

Siamo spiacenti. Ho incollato il codice in fretta. Rimuovi "assert 0 == 1" dalla funzione "test_one" per notare il "problema". Solo quando c'è un errore (che ho forzato avendo una falsa affermazione), py.test sembra stampare le informazioni di registrazione. – superselector

+0

Nessun problema, ho scoperto come risolvere sulla riga di comando, cercando un modo programmatico. – TryPyPy

+1

si potrebbe anche reindirizzare l'output di registrazione ad alcuni file invece di stderr implicita di default. – hpk42

Problemi correlati