Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nose plugin for expected-failures

Is there an existing plugin which could be used like:

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

If the test fails, it would appear like a skipped test:

$ nosetests
...S..

..but if it unexpected passes, it would appear similarly to a failure, maybe like:

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

Kind of like SkipTest, but not implemented as an exception which prevents the test from running.

Only thing I can find is this ticket about supporting the unittest2 expectedFailure decorator (although I'd rather not use unittest2, even if nose supported it)

like image 353
dbr Avatar asked Mar 08 '12 06:03

dbr


3 Answers

You can do it with one of two ways:

  1. nose.tools.raises decorator

    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")
    

Tests will fail if exception is not raised.

Yup, I know, asked 3 years ago :)

like image 107
voy Avatar answered Oct 13 '22 06:10

voy


I don't know about a nose plugin, but you could easily write your own decorator to do that. Here's a simple implementation:

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

If I run these tests:

@expected_failure
def test_not_implemented():
    assert False

@expected_failure
def test_unexpected_success():
    assert True

I get the following output from nose:

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)
like image 11
aquavitae Avatar answered Oct 26 '22 05:10

aquavitae


Forgive me if I've misunderstood, but isn't the behavior you desire provided by core python's unittest library with the expectedFailure decorator, which is—by extension—compatible with nose?

For an example of use see the docs and a post about its implementation.

like image 4
Griffith Rees Avatar answered Oct 26 '22 07:10

Griffith Rees