API

younit is a collection of helpers for the unittest module.

younit.test_name(func)

A decorator that prints the test name before starting a test.

Convenient if you want to separate the output of different tests.

Usage:

class MyTestCase(unittest.TestCase):
    @test_name
    def test_this(self):
        print("im testing this")

    @test_name
    def test_that(self):
        print("im testing that")
younit.set_test_hang_alarm(func)

A decorator that sets an alarm of 1 second before starting any test.

If a test takes longer than 1 second, a TestHang exception is raised.

Should be used during set up and in conjunction with clear_test_hang_alarm() during tear down.

Usage:

class MyTestCase(unittest.TestCase):
    @set_test_hang_alarm
    def setUp(self):
        pass

    @clear_test_hang_alarm
    def tearDown(self):
        pass
younit.clear_test_hang_alarm(func)

A decorator that resets an alarm set by set_test_hang_alarm()

Should be used during tear down and in conjunction with set_test_hang_alarm() during set up.

Usage:

class MyTestCase(unittest.TestCase):
    @set_test_hang_alarm
    def setUp(self):
        pass

    @clear_test_hang_alarm
    def tearDown(self):
        pass
younit.test_hang_alarm(func)

A decorator that sets an alarm of 1 second before starting any test.

If a test takes longer than 1 second, a TestHang exception is raised.

If a test takes less than 1 second, the alarm is cancelled.

Usage:

class MyTestCase(unittest.TestCase):

    @test_hang_alarm
    def test_this(self):
        time.sleep(3)
younit.close_all_threads(func)

A decorator that closes any threads that are created as part of running a test.

To use, ensure your threads are able to be closed by invoking a close() method on an object related to the thread. Then add the object to the self.threads_to_close list.

Usage:

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.threads_to_close = []
        x = start_a_new_thread()
        #x is an object with a close() method
        #that closes the thread
        self.threads_to_close.append(x)

    @close_all_threads
    def test_this(self):
        y = start_a_new_thread()
        self.threads_to_close.append(y)
younit.asyncio_test(func)

A decorator that runs a test as a coroutine including any set up and tear down coroutines.

Usage:

class MyTestCase(unittest.TestCase):
    async def async_setUp(self):
        pass

    async def async_tearDown(self):
        pass

    @asyncio_test
    async def test_this(self):
        pass
younit.AsyncMock(*args, **kwargs)

A function that can be used to mock a coroutine.

Returns a coroutine function with a mock attribute. The mock attribute is a unittest.mock.MagicMock object that records usage.

Usage:

class MyTestCase(unittest.TestCase):
    async def async_setUp(self):
        pass

    async def async_tearDown(self):
        pass

    @asyncio_test
    async def test_this(self):
        x = AsyncMock()
        await x()
        x.mock.assert_called_once()
exception younit.TestHang