pytest_runtest_makereport of custom conftest doesn't get called when running with xdist with -n option
I have noticed registering new plugin having the method (item, call)pytest_runtest_makereport doesn't get call back. However it works without -n option.
Here is my sample conftest.
def pytest_configure(config):
config._foo = FooBar()
config.pluginmanager.register(config._foo)
def pytest_unconfigure(config):
foo = getattr(config, '_foo', None)
if foo:
del config._foo
config.pluginmanager.unregister(foo)
class FooBar(object):
def pytest_runtest_makereport(self, item, call):
print "pytest_runtest_makereport"
Sample test file
class Test_Foo_Bar(object):
def test_foo_bar_01(self):
"""
test_foo_bar_01
"""
assert 2 == 2
@pytest.mark.xfail(reason="passed Simply")
def test_foo_bar_02(self):
"""
test_foo_bar_02
"""
assert 3 == 3
Can some one look into this issue please.
Unfortunately printing on the slaves does not work because stdout is used for communication by execnet in the current implementation.
I changed your example slightly to write to a file instead:
# conftest.py
import os
def pytest_configure(config):
config._foo = FooBar()
config.pluginmanager.register(config._foo)
if os.path.isfile('report'):
os.remove('report')
def pytest_unconfigure(config):
foo = getattr(config, '_foo', None)
if foo:
del config._foo
config.pluginmanager.unregister(foo)
class FooBar(object):
def pytest_runtest_makereport(self, item, call):
with open('report', 'a+') as f:
f.write(call.when + '\n')
# test_foo.py
class Test_Foo_Bar(object):
def test_foo_bar_01(self):
"""
test_foo_bar_01
"""
assert 2 == 2
$ py.test test_foo.py -n2 -q
gw0 [1] / gw1 [1]
scheduling tests via LoadScheduling
.
1 passed in 2.56 seconds
$
$ cat report
setup
call
teardown
closing this one as missunderstanding on the op side, please notify if there is something missing
We should document this though, as it is a common catch: #82.
@RonnyPfannschmidt If i prevent running on slave node i get the issue which i reported.
Actutally pytest_runtest_logreport in this case but not pytest_runtest_makereport.
I have made some tweaks and here is my sample code.
# conftest.py
import os
def pytest_configure(config):
if not hasattr(config, 'slaveinput'):
config._foo = FooBar()
config.pluginmanager.register(config._foo)
if os.path.isfile('report'):
os.remove('report')
def pytest_unconfigure(config):
foo = getattr(config, '_foo', None)
if foo:
del config._foo
config.pluginmanager.unregister(foo)
class FooBar(object):
def pytest_runtest_makereport(self, item, call):
with open('report', 'a+') as f:
f.write(call.when + '\n')
def pytest_runtest_logreport(self, report):
with open('pytest_runtest_logreport', 'a+') as f:
f.write(report.when + '\n')
@nicoddemus @RonnyPfannschmidt My aim is to get access to both item and report even using xdist plugin.
@ssrikanta by design the makereport happens only on the slave - item is never ever transfered to the master
so by intended design the plugin you posted can not work
@RonnyPfannschmidt Is there any alternative where can i access both item and report
Also i have observed pytest_sessionfinish gets called multiple times when using xdist, IS there way to make it call only once after complete finish of session
@ssrikanta its by design impossible to access item on the master - what do you want to use it for?
@ssrikanta the session finish hook happens on every node also by design, you can figure if you are on the master by taking alook at session.config
I need an reporter with test status and also i need to capture the doc string available with each test.
I can capture the doc string with item object
so put the docstring into the report as a section
But to do that i need a place where i can access both report and item rite, so which can run with xdist as well
you put the section into the report in a hookwrapper for the makereport hook, then you can access it in the logreport hook
Currently i am getting the results and test doc string in makereport hook and writing it to a file in session_finish hook, but the problem here is, if i wont restrict to run on slave nodes, session_finish will get called more than once.
all nodes (masters and slaves) run session_finish