2012-03-08 2 views
8

C'è un plugin esistente che potrebbe essere utilizzato come:naso plug-in per-fallimenti attesi

@nose.plugins.expectedfailure 
def not_done_yet(): 
    a = Thingamajig().fancynewthing() 
    assert a == "example" 

Se il test fallisce, sembrerebbe come una prova saltata:

$ nosetests 
...S.. 

..ma se passa inaspettati, sembrerebbe in modo simile a un fallimento, forse come:

================================= 
UNEXPECTED PASS: not_done_yet 
--------------------------------- 
-- >> begin captured stdout << -- 
Things and etc 
... 

Tipo di like SkipTest, ma non cuzione mentata come un'eccezione che impedisce al test di funzionare.

unica cosa che posso trovare è this ticket di sostenere il decoratore unittest2 expectedFailure (anche se preferirei non uso unittest2, anche se il naso è supportato)

risposta

11

Non so su un plugin naso, ma si potrebbe scrivi facilmente il tuo decoratore per farlo. Ecco una semplice implementazione:

import functools 
import nose 

def expected_failure(test): 
    @functools.wraps(test) 
    def inner(*args, **kwargs): 
     try: 
      test(*args, **kwargs) 
     except Exception: 
      raise nose.SkipTest 
     else: 
      raise AssertionError('Failure expected') 
    return inner 

Se corro questi test:

@expected_failure 
def test_not_implemented(): 
    assert False 

@expected_failure 
def test_unexpected_success(): 
    assert True 

ricevo il seguente uscita dal naso:

tests.test.test_not_implemented ... SKIP 
tests.test.test_unexpected_success ... FAIL 

====================================================================== 
FAIL: tests.test.test_unexpected_success 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\Python32\lib\site-packages\nose-1.1.2-py3.2.egg\nose\case.py", line 198, in runTest 
    self.test(*self.arg) 
    File "G:\Projects\Programming\dt-tools\new-sanbi\tests\test.py", line 16, in inner 
    raise AssertionError('Failure expected') 
AssertionError: Failure expected 

---------------------------------------------------------------------- 
Ran 2 tests in 0.016s 

FAILED (failures=1) 
+0

Oh, certo! Con un piccolo aggiustamento quindi solleva SkipTest quando il test fallisce, è perfetto - grazie \ o/ – dbr

3

Perdonami se ho capito male, ma isn il comportamento che desideri fornito dalla libreria unittest di core python con il decoratore expectedFailure, che è compatibile con l'estensione-nose?

Per un esempio di utilizzo vedere docs e post about its implementation.

+0

Sì, è vero, ma una delle cose che mi piacciono del naso è la capacità di scrivere test come funzioni, piuttosto che i metodi su sottoclassi come è richiesto dal modulo unittest incorporato (ad es. 'def test_exampleblah(): pass') – dbr

+2

Se questo è il problema allora forse ti piacerebbe [' pytest'] (http://pytest.org/latest/contents.html), che è [compatibile con 'nose'] (http://pytest.org/latest/nose.html), supporta anche [test come funzioni] (http://pytest.org/latest/assert.html#asserting-with -the-assert-statement) e ha il decoratore ['xfail'] (http://pytest.org/latest/skipping.html#mark-a-test-function-as-expected-to-fail). –

+2

Nella mia esperienza, 'unittest.expectedFailure' è * non * compatibile con Naso. [Naso bug 33] (https: // github.com/nose-devs/nose/issues/33) concorda. –

-2

È possibile farlo con uno dei due modi:

  1. nose.tools.raises decoratore

    from nose.tools import raises 
    
    @raises(TypeError) 
    def test_raises_type_error(): 
        raise TypeError("This test passes") 
    
  2. nose.tools.assert_raises

    from nose.tools import assert_raises 
    
    def test_raises_type_error(): 
        with assert_raises(TypeError): 
         raise TypeError("This test passes") 
    

test avrà esito negativo se un'eccezione non è sollevato

Sì, lo so, chiesto 3 anni fa :)