Quick actions

cmd+k|ctrl+k

Navigation

Languages

python-unittest-check for exceptions

Snippet info

Language

Python

Visibility

public

Author

rednik96

Created

2018-04-19T13:16:53Z

Updated

2018-04-19T13:16:53Z

# coding: utf-8

# https://stackoverflow.com/questions/129507/how-do-you-test-that-a-python-function-throws-an-exception

import unittest


def test(a):
    assert a >=0


class A(unittest.TestCase):
    
    def test_one(self):
        with self.assertRaises(AssertionError) as context:
            test(-5)

        # print context
        self.assertTrue(isinstance(context.exception, AssertionError))
        print('erfolg')
        
a = A()
a.test_one()
INFO