87 lines
2.8 KiB
Plaintext
87 lines
2.8 KiB
Plaintext
|
|
pytest_execnetcleanup plugin
|
|
============================
|
|
|
|
cleanup execnet gateways during test function runs.
|
|
|
|
|
|
|
|
Getting and improving this plugin
|
|
---------------------------------
|
|
|
|
|
|
Do you find the above documentation or the plugin itself lacking,
|
|
not fit for what you need? Here is a **30 seconds guide**
|
|
to get you started on improving the plugin:
|
|
|
|
1. Download `pytest_execnetcleanup.py`_ plugin source code
|
|
2. put it somewhere as ``pytest_execnetcleanup.py`` into your import path
|
|
3. a subsequent test run will now use your local version!
|
|
|
|
Further information: extend_ documentation, other plugins_ or contact_.
|
|
|
|
For your convenience here is also an inlined version of ``pytest_execnetcleanup.py``:
|
|
|
|
.. sourcecode:: python
|
|
|
|
"""
|
|
cleanup execnet gateways during test function runs.
|
|
"""
|
|
import py
|
|
|
|
pytest_plugins = "xfail"
|
|
|
|
def pytest_configure(config):
|
|
config.pluginmanager.register(Execnetcleanup())
|
|
|
|
class Execnetcleanup:
|
|
_gateways = None
|
|
def __init__(self, debug=False):
|
|
self._debug = debug
|
|
|
|
def pyexecnet_gateway_init(self, gateway):
|
|
if self._gateways is not None:
|
|
self._gateways.append(gateway)
|
|
|
|
def pyexecnet_gateway_exit(self, gateway):
|
|
if self._gateways is not None:
|
|
self._gateways.remove(gateway)
|
|
|
|
def pytest_sessionstart(self, session):
|
|
self._gateways = []
|
|
|
|
def pytest_sessionfinish(self, session, exitstatus):
|
|
l = []
|
|
for gw in self._gateways:
|
|
gw.exit()
|
|
l.append(gw)
|
|
#for gw in l:
|
|
# gw.join()
|
|
|
|
def pytest_pyfunc_call(self, __call__, pyfuncitem):
|
|
if self._gateways is not None:
|
|
gateways = self._gateways[:]
|
|
res = __call__.execute(firstresult=True)
|
|
while len(self._gateways) > len(gateways):
|
|
self._gateways[-1].exit()
|
|
return res
|
|
|
|
def test_execnetplugin(testdir):
|
|
reprec = testdir.inline_runsource("""
|
|
import py
|
|
import sys
|
|
def test_hello():
|
|
sys._gw = py.execnet.PopenGateway()
|
|
def test_world():
|
|
assert hasattr(sys, '_gw')
|
|
py.test.raises(KeyError, "sys._gw.exit()") # already closed
|
|
|
|
""", "-s", "--debug")
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
.. _`pytest_execnetcleanup.py`: http://bitbucket.org/hpk42/py-trunk/raw/ea1f958813ebbff45161fdb468a6204be5396112/py/test/plugin/pytest_execnetcleanup.py
|
|
.. _`extend`: ../extend.html
|
|
.. _`plugins`: index.html
|
|
.. _`contact`: ../../contact.html
|
|
.. _`checkout the py.test development version`: ../../download.html#checkout
|