To run tests, they must be collected in a Tests instance. There are many ways this can be achieved, allowing flexibility and separation.
Collection of test functions.
Parameters: |
|
---|
New in version 0.6: Pass a single string to tests without wrapping it in an iterable.
New in version 0.6: The replace_tests and replace_contexts parameters. The decorator methods now default to simply registering functions and leaving the original in place. This allows functions to be decorated and registered with multiple collections easily.
Run all tests in this collection.
Parameters: |
|
---|
Changed in version 0.6: Added full_tracebacks and fail_fast.
Interface to run() with command-line options.
Remaining arguments are passed to the reporter.
New in version 0.2.
Changed in version 0.4: --list-reporters was added.
Changed in version 0.6: --full-tracebacks was added.
Decorate a function as a contextmanager() for running the tests in this collection in. Corresponds to setup and teardown in other testing libraries.
db = Tests()
@db.context
def connect():
con = connect_db()
try:
yield con
finally:
con.disconnect()
@db.test
def using_connection(con):
assert con is not None
The above corresponds to:
db = Tests()
@contextmanager
def connect():
con = connect_db()
try:
yield con
finally:
con.disconnect()
@db.test
def using_connection():
with connect() as con:
assert con is not None
The difference is that this decorator applies the context to all tests defined in its collection, so it’s less repetitive.
Yielding None or nothing passes no arguments to the test, yielding a single value other than a tuple passes that value as the sole argument to the test, yielding a tuple splats the tuple as the arguments to the test. If you want to yield a tuple as the sole argument, wrap it in a one-tuple or unsplat the args in the test.
You can have more than one context, which will be run in order using contextlib.nested(), and their yields will be passed in order to the test functions.
New in version 0.2: Nested contexts.
Changed in version 0.5: Tests will gets as many arguments as they ask for.
Merge in other tests.
Parameters: | tests –
|
---|
Any of these can be passed in a list to the Tests constructor.
New in version 0.2: Refer to collections by import path as a string
New in version 0.6: Recursive scanning of modules and packages
Changed in version 0.6: Tests are only added if not already added
Returns register() if the condition is True.
New in version 0.4.
Create a unittest.TestSuite from this collection.
Base for test classes. Decorate test methods with test(). Needs to be registered with a Tests collection to be run. For setup and teardown, override __context__() like a contextmanager() (without the decorator).
class Math(TestBase):
def __context__(self):
self.two = 1 + 1
yield
del self.two
@test
def arithmetics(self):
assert self.two == 2
suite = Tests([Math()])
suite.run()