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: |
|
---|
Run all tests in this collection.
Parameters: | reporter – An instance of AbstractReporter or a callable returning something implementing that API (not enforced). |
---|
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.
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 another test collection.
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
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()