A setup/teardown context manager decorator for unit tests
Project description
Setup Teardown Content Decorator
This is a simple package that aims to make adding setup and teardown to pytest flavored tests quick and painless.
Installation
python3 -m pip install setup-teardown
Decorator Usage
Custom Database Class
from setup_teardown import SetupTeardown
class PgSetupTeardown(SetupTeardown):
def __init__(self, table, **kwargs):
self.table = table
self.__dict__.update(kwargs)
def __enter__(self):
# Perform test setup
self.session = db.session.new_session()
self.session.query(self.table).delete()
self.session.commit()
return self
def __exit__(self, typ, val, traceback):
# Perform test teardown
self.session.query(self.table).delete()
self.session.commit()
Actual test
class TestHandlerDatabaseRequired:
@SetupTeardown(table="table_name")
def test_handler_success(self, mock_datetime):
"""
Example of using the SetupTeardown ContextDecorator with arbitrary setup and teardown
"""
# execute the test
pass
Context manager usage
def setup(self):
session = db.session.new_session()
session.query(self.table).delete()
session.commit()
def teardown(self):
session = db.session.new_session()
session.query(self.table).delete()
session.commit()
# contest manager application example
with SetupTeardown(setup=setup, teardown=teardown):
print("inner")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
setup-teardown-0.0.3.tar.gz
(2.3 kB
view hashes)
Built Distribution
Close
Hashes for setup_teardown-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4af8abd38948a3cafb2ac656f3f0f31e4bef163dbb457d55140e4e62bfeefd2f |
|
MD5 | 3b46fbf082e6dfd8141e35ec5c49f6f2 |
|
BLAKE2b-256 | 1b0a2458c2c96bce334e518c27252db5ee98c79ad52a917aa6f221b8b54a4b7e |