.. _apps-test: .. module:: pulsar.apps.test ############## Test Suite ############## The :class:`TestSuite` is a testing framework for both synchronous and asynchronous applications and for running tests in parallel on multiple threads or processes. It is used for testing pulsar but it can be used as a test suite for any other library. .. contents:: :depth: 3 .. _apps-test-intro: =============== Integration =============== Pulsar test suite can be used in conjunction with setuptools_ or stand-alone. Setuptools Integration =========================== Pulsar asynchronous test suite can be used as testing framework in your setuptools based project. Add this to setup.py file:: from setuptools import setup setup( #..., setup_requires=['pulsar', ...], #..., ) And create an alias into ``setup.cfg`` file:: [aliases] test=pulsar_test If you now type:: python setup.py test this will execute your tests using pulsar test runner. As this is a standalone version of pulsar no prior installation whatsoever is required for calling the test command. You can also pass additional arguments to pulsar test, such as your test directory or other options using -a. Manual Integration =========================== If for some reason you don’t want/can’t use the setuptools integration, you can write a standalone script, for example ``runtests.py``:: from pulsar.apps import TestSuite if __name__ == '__main__': TestSuite(description='Test suite for my library').start() To run the test suite:: python runtests.py Type:: python runtests.py --help For a list different options/parameters which can be used when running tests. =========================== Running Tests =========================== Writing a Test Case =========================== Only subclasses of :class:`~unittest.TestCase` are collected by this application. An example test case:: import unittest class MyTest(unittest.TestCase): async def test_async_test(self): result = await async_function() self.assertEqual(result, ...) def test_simple_test(self): self.assertEqual(1, 1) .. note:: Test functions are asynchronous, when they are coroutine functions or return a :class:`~asyncio.Future`, synchronous, when they return anything else. .. _apps-test-loading: Loading Tests ================= The loading of test cases is controlled by the ``modules`` parameter when initialising the :class:`TestSuite`:: from pulsar.apps import TestSuite if __name__ == '__main__': TestSuite(modules=('tests', 'examples')).start() When using pulsar test suite - setuptools_ integration, test modules are specified in the ``setup.cfg``:: [test] test_modules = tests examples Test files =========================== The :class:`.TestSuite` loads tests via the :attr:`~.TestSuite.loader` property. By default it recursively loads test files inside the ``modules`` directories matching: * ``test_