2016-04-20 27 views
9

Sto cercando un modo per eseguire tutti i miei test di unità in PyTest, anche se alcuni di essi falliscono. So che ci deve essere un modo semplice per farlo. Ho controllato le opzioni CLi e ho guardato attraverso questo sito per domande/risposte simili ma non ho visto nulla. Scusate se è già stata data una risposta.Come eseguire tutti i test PyTest anche se alcuni di essi falliscono?

Ad esempio, si consideri il seguente frammento di codice, con il codice di PyTest accanto ad esso:

def parrot(i): 
    return i 

def test_parrot(): 
    assert parrot(0) == 0 
    assert parrot(1) == 1 
    assert parrot(2) == 1 
    assert parrot(2) == 2 

Per impostazione predefinita, l'esecuzione si ferma al primo fallimento:

$ python -m pytest fail_me.py 
=================== test session starts =================== 
platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 
rootdir: /home/npsrt/Documents/repo/codewars, inifile: 
collected 1 items 

fail_me.py F 

=================== FAILURES =================== 
___________________ test_parrot ___________________ 

    def test_parrot(): 
     assert parrot(0) == 0 
     assert parrot(1) == 1 
>  assert parrot(2) == 1 
E  assert 2 == 1 
E  + where 2 = parrot(2) 

fail_me.py:7: AssertionError 
=================== 1 failed in 0.05 seconds =================== 

Quello che vorrei fare è che il codice continui ad essere eseguito anche dopo che PyTest riscontra il primo errore.

+0

Vedi anche [questa domanda] (https://stackoverflow.com/q/4732827/102441) per 'unittest', che è collegato a un gruppo di domande molto simili – Eric

risposta

9

Ha eseguito tutti i test. Hai scritto solo un test e quel test è stato eseguito!

Se si desidera un'asserzione non fatale, in cui un test andrà avanti se un'asserzione fallisce (come i macro EXPECT di Google Test), provare con pytest-expect, che fornisce tale funzionalità. Ecco l'esempio loro sito dà:

def test_func(expect): 
    expect('a' == 'b') 
    expect(1 != 1) 
    a = 1 
    b = 2 
    expect(a == b, 'a:%s b:%s' % (a,b)) 

si può vedere che i guasti di aspettativa non interrompere il test, e tutti fallito le aspettative vengono segnalati:

$ python -m pytest test_expect.py 
================ test session starts ================= 
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.7.0 
rootdir: /Users/okken/example, inifile: 
plugins: expect 
collected 1 items 

test_expect.py F 

====================== FAILURES ====================== 
_____________________ test_func ______________________ 
> expect('a' == 'b') 
test_expect.py:2 
-------- 
> expect(1 != 1) 
test_expect.py:3 
-------- 
> expect(a == b, 'a:%s b:%s' % (a,b)) 
a:1 b:2 
test_expect.py:6 
-------- 
Failed Expectations:3 
============== 1 failed in 0.01 seconds ============== 
+0

Aha! Questo risponde alla mia domanda. Stavo semplicemente eseguendo un test, che conteneva più affermazioni al suo interno. Controllerò anche il modulo pytest-expect. –

+0

Nota che [lo sviluppo su 'pytest-expect'] (https://github.com/okken/pytest-expect) è andato piuttosto stantia – Eric

5

Si dovrebbe essere in grado di controllare questo con l'argomento --maxfail. Credo che l'impostazione predefinita sia quella di non fermarsi per errori, quindi controllerei qualsiasi file di configurazione py.test che potresti avere per un posto che lo sovrascrive.

+0

Grazie per la tua risposta rapida. (Si prega di vedere la mia domanda originale aggiornata sopra per informazioni sul mio ambiente.) Sfortunatamente, non sembra funzionare per me. PyTest mi dà lo stesso risultato quando invoco --maxfail come quando corro senza di esso. La mia nuova riga di comando è: python -m pytest --maxfail = 5 fail_me.py –

+0

'--maxfail' determina il numero di _test_ per fallire, non quanti' assert'ions – Eric

7

Come altri già citati, si sarebbe idealmente scrivi più test e hai solo un'asserzione in ognuno (non è un limite difficile, ma una buona linea guida).

Il @pytest.mark.parametrize decoratore rende questo facile:

import pytest 

def parrot(i): 
    return i 

@pytest.mark.parametrize('inp, expected', [(0, 0), (1, 1), (2, 1), (2, 2)]) 
def test_parrot(inp, expected): 
    assert parrot(inp) == expected 

Quando si esegue con -v:

parrot.py::test_parrot[0-0] PASSED 
parrot.py::test_parrot[1-1] PASSED 
parrot.py::test_parrot[2-1] FAILED 
parrot.py::test_parrot[2-2] PASSED 

=================================== FAILURES =================================== 
_______________________________ test_parrot[2-1] _______________________________ 

inp = 2, expected = 1 

    @pytest.mark.parametrize('inp, expected', [(0, 0), (1, 1), (2, 1), (2, 2)]) 
    def test_parrot(inp, expected): 
>  assert parrot(inp) == expected 
E  assert 2 == 1 
E  + where 2 = parrot(2) 

parrot.py:8: AssertionError 
====================== 1 failed, 3 passed in 0.01 seconds ====================== 
+0

Grazie, guarderò dentro –