Code Monkey home page Code Monkey logo

xfail.py's Introduction

XFail

image

image

image

image

XFail provides a decorator function xfail to skip expected exceptions. Similar to unittest.skipIf, but xfail can specify which exception should be skipped, and raise if the decorated function is unexpectedly passed (only if strict is True).

Install

Supported python versions are: 2.7, 3.4, 3.5, 3.6

Can be installed from PyPI by pip:

pip install xfail

Usage

xfail decorator

xfail accepts two arguments, exceptions and strict.

The first argument (exceptions) should be an Exception class to skip (Ex. Exception, AssertionError, and so on). If you want to skip multiple exceptions, use tuple of them, for example, @xfail((AssertionError, ValueError)).

The second argument strict should be a boolean. If strict is False (by default) and passed unexpectedly, raise unittest.SkipTest exception, which will mark the test is skipped. This case is very similar to the function is decorated by uniteest.skip function and the test will be counted as skipped.

If it is True and the decorated function did not raise (any of) the expected exception(s), XPassFailure exception would be raised. In this case, the test will be counted as fail.

from xfail import xfail

@xfail(IndexError)
def get(l, index):
    return l[index]

l = [1, 2, 3]
get(4)  # no error

Also supports multiple exceptions:

@xfail((IndexError, ValueError))
def a():
    '''This function passes IndexError and ValueError
    ...

In test script, similar to unittest.TestCase.assertRaises:

from unittest import TestCase
from xfail import xfail

class MyTest(TestCase):
    def test_1(self):
        @xfail(AssertionError)
        def should_raise_error():
            assert False
        a()  # test passes

    def test_2(self):
        @xfail(AssertionError, strict=True)
        def should_raise_error():
            assert True
        a()  # test failes, since this function should raise AssertionError

    # Can be used for test function
    @xfail(AssertionError, strict=True)
    def test_3(self)
        assert False

    # This test will fail
    @xfail(AssertionError, strict=True)
    def test_3(self)
        assert True

For more exapmles, see test_xfail.py.

xfail.py's People

Contributors

miyakogi avatar

Stargazers

fx-kirin avatar

Watchers

James Cloos avatar  avatar  avatar

xfail.py's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.