I have the following problem. I have multiple unittests, each one having a context manager to open a browser and do some selenium testing.
I want to ensure that I can run the tests in parallel, and that the browser window is closed in case of error, so I use a context manager:
def test_xxx():
with webapp() as p:
file_loader = FileLoader(p, id="loader").upload(path)
As you can see, I use a class FileLoader, which takes the context manager web application (basically a wrapper of the selenium driver) and uses it to encapsulate the rigmarole required to upload the file.
My objective would be not to have to specify the p parameter to FileLoader(), so that I could write
def test_xxx():
with webapp():
file_loader = FileLoader(id="loader").upload(path)
I could use a global that is assigned when the context manager is opened, but this would prevent any isolation when tests run in parallel. Suppose that one test connects to site A, and another test connects to site B. I need two drivers, each connected to a different site.
In other words, how can I design FileLoader to be aware of its enclosing context manager without passing the context variable?