assamtest

mocha-style tests for python
git clone https://git.ce9e.org/assamtest.git

commit
d9906c9ebd648e5c9818fc929ccda9a82ac831d2
parent
5f1d8cc65d3800762bf0bf5d2bb00b36678ad4b6
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-02-09 13:02
add README

Diffstat

A README.rst 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1 files changed, 127 insertions, 0 deletions


diff --git a/README.rst b/README.rst

@@ -0,0 +1,127 @@
   -1     1 .. py:decorator:: test(name=None, args=[], decorators=[])
   -1     2 
   -1     3    Register a function as a test::
   -1     4 
   -1     5       import assamtest
   -1     6       from assamtest import expect
   -1     7 
   -1     8       @assamtest.test(args=['+', 5])
   -1     9       @assamtest.test(args=['*', 6])
   -1    10       def my_test(op, value):
   -1    11          assamtest.expect.equal(eval('2 %s 3' % op), value)
   -1    12 
   -1    13    :param str name: The name of this test (defaults to the function name)
   -1    14    :param list args: Arguments that should be passed to the test function
   -1    15    :param list decorators: The test function will be passed through these decorators before being executed.
   -1    16 
   -1    17 .. py:decorator:: suite(name=None, args=[], decorators=[])
   -1    18 
   -1    19    Register a function as a suite::
   -1    20 
   -1    21       import assamtest
   -1    22       from assamtest import expect
   -1    23 
   -1    24       @assamtest.suite()
   -1    25       def my_suite():
   -1    26          @assamtest.before_each()
   -1    27          def _before_each():
   -1    28             pass  # do some setup here
   -1    29 
   -1    30          @assamtest.test()
   -1    31          def my_test():
   -1    32             expect.equal(2 + 2, 4)
   -1    33 
   -1    34    The optional parameters are the same as for :py:func:`test`.
   -1    35 
   -1    36 .. py:decorator:: before()
   -1    37 .. py:decorator:: after()
   -1    38 
   -1    39    Register a function to run before/after the whole suite.
   -1    40 
   -1    41    There can be only one before/after function per suite.
   -1    42 
   -1    43 .. py:decorator:: before_each()
   -1    44 .. py:decorator:: after_each()
   -1    45 
   -1    46    Register a function to run before/after every test.
   -1    47 
   -1    48    There can be only one before_each/after_each function per suite.
   -1    49 
   -1    50 .. py:data:: expect
   -1    51 
   -1    52    A wrapper around the asserts from :py:class:`unittest.TestCase` using snake
   -1    53    case::
   -1    54 
   -1    55       from assamtest import expect
   -1    56 
   -1    57       expect.equal(2 + 2, 4)
   -1    58       expect.not_equal(2 + 2, 5)
   -1    59       expect._in(2, [1, 2, 3])
   -1    60       with expect.raises(KeyError):
   -1    61          {'foo': 0}['bar']
   -1    62 
   -1    63    See also the `full list of available assertions
   -1    64    <https://docs.python.org/3/library/unittest.html?highlight=unittest%20testcase#assert-methods>`_.
   -1    65 
   -1    66 .. py:exception:: Outcome(err, status, level)
   -1    67 
   -1    68    Can be used to implement custom outcomes.
   -1    69 
   -1    70    :param Exception|str|None err: the reason for this outcome, e.g. an exception or a helpful message
   -1    71    :param str status: the status, e.g. "passed", "failed", or "skipped"
   -1    72    :param 'SUCCESS'|'INFO'|'WARNING'|'ERROR' level: a hint for the reporter how this outcome should be interpreted
   -1    73 
   -1    74    A good example of how this can be used is :py:func:`decorators.skip`::
   -1    75 
   -1    76       import functools
   -1    77       from assamtest import Outcome
   -1    78 
   -1    79       def skip(fn):
   -1    80          @functools.wraps(fn)
   -1    81          def wrapper(*args, **kwargs):
   -1    82             raise Outcome(None, 'skipped', 'INFO')
   -1    83          return wrapper
   -1    84 
   -1    85 .. py:module:: decorators
   -1    86 
   -1    87 .. py:decorator:: skip
   -1    88 
   -1    89    Do not execute the test at all::
   -1    90 
   -1    91       import assamtest
   -1    92       from assamtest import expect
   -1    93       from assamtest.decorators import skip
   -1    94 
   -1    95       @assamtest.test(decorators=[skip])
   -1    96       def my_test():
   -1    97          expect.equal(2 + 2, 5)
   -1    98 
   -1    99 .. py:decorator:: fail
   -1   100 
   -1   101    Invert the result of the test: If it would fail, pass instead. If it would
   -1   102    pass, fail instead::
   -1   103 
   -1   104       import assamtest
   -1   105       from assamtest import expect
   -1   106       from assamtest.decorators import fail
   -1   107 
   -1   108       @assamtest.test(args=[4])
   -1   109       @assamtest.test(args=[5], decorators=[fail])
   -1   110       def my_test(value):
   -1   111          expect.equal(2 + 2, value)
   -1   112 
   -1   113 .. py:decorator:: synchronize
   -1   114 
   -1   115    Start an asyncio event loop for the test and wait for it to complete::
   -1   116 
   -1   117       import asyncio
   -1   118 
   -1   119       import assamtest
   -1   120       from assamtest import expect
   -1   121       from assamtest.decorators import synchronize
   -1   122 
   -1   123       @assamtest.test()
   -1   124       @synchronize
   -1   125       async def my_test():
   -1   126          await asyncio.sleep(0.1)
   -1   127          expect.equal(2 + 2, 4)