I would like to embed a test library into robotframework that itself is using a context manager library to provide test resources.
I am planning to write a library using the Listener Interface that embeds the context manager __enter__
and __exit__
methods to robot steps Setup
and Teardown
. (For the first time on test suite setup/teardown, but i think it can be extended for test or keyword scale as well).
I was wondering that there is no 'ready to go' functionality for this, since I see similar techniques in robot framework code: Setup/Teardown. I like to avoid to make a setup keyword and a teardown keyword, since e.g. on error case the teardown keyword could be skipped -- and of course is boiler code. Nicer in my eyes would be the auto deconstruction on test (suite) end.
Are there some other ideas to embed context manager libs into test libraries?
This idea will look like this:
from typing import ContextManager
from robot.running.model import TestSuite as TestSuiteModel
from robot.result.model import TestSuite as TestSuiteResult
class SuiteManagedContextManager:
'''
Base class embedding pythons context manager classes into robot framework listener library.
'''
def __init__(self, context_manager: ContextManager) -> None:
self._context_manager = context_manager
def start_suite(self, data: TestSuiteModel, result: TestSuiteResult) -> None:
'''
Robot Library Listener interface v3.
'''
assert self._context_manager
self._context_manager.__enter__()
def end_suite(self, data: TestSuiteModel, result: TestSuiteResult) -> None:
'''
Robot Library Listener interface v3.
'''
assert self._context_manager
self._context_manager.__exit__(None, None, None)
@library(scope='Test Suite', doc_format='reST')
class MyResourceLib(SuiteManagedContextManager):
'''
User Test Library making use of context manager: `MyDBContextManger`
'''
ROBOT_LISTENER_API_VERSION = 3
class __init__(self, db_resource: Optional[MyDBContextManger] = None):
self.ROBOT_LIBRARY_LISTENER = self
if not db_resource:
self.db_resource = MyDBContextManger()
else:
self.db_resource = db_resource
super.__init__(db_resource)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…