pytest-xdist icon indicating copy to clipboard operation
pytest-xdist copied to clipboard

pytest_runtest_makereport of custom conftest doesn't get called when running with xdist with -n option

Open ssrikanta-scea opened this issue 10 years ago • 21 comments

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.

ssrikanta-scea avatar Aug 06 '16 05:08 ssrikanta-scea

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"


ssrikanta-scea avatar Aug 12 '16 09:08 ssrikanta-scea

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

ssrikanta-scea avatar Aug 12 '16 09:08 ssrikanta-scea

Can some one look into this issue please.

ssrikanta-scea avatar Aug 12 '16 09:08 ssrikanta-scea

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

nicoddemus avatar Aug 12 '16 11:08 nicoddemus

closing this one as missunderstanding on the op side, please notify if there is something missing

RonnyPfannschmidt avatar Aug 12 '16 11:08 RonnyPfannschmidt

We should document this though, as it is a common catch: #82.

nicoddemus avatar Aug 12 '16 11:08 nicoddemus

@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')

ssrikanta avatar Aug 18 '16 05:08 ssrikanta

@nicoddemus @RonnyPfannschmidt My aim is to get access to both item and report even using xdist plugin.

ssrikanta avatar Aug 18 '16 05:08 ssrikanta

@ssrikanta by design the makereport happens only on the slave - item is never ever transfered to the master

RonnyPfannschmidt avatar Aug 18 '16 05:08 RonnyPfannschmidt

so by intended design the plugin you posted can not work

RonnyPfannschmidt avatar Aug 18 '16 05:08 RonnyPfannschmidt

@RonnyPfannschmidt Is there any alternative where can i access both item and report

ssrikanta avatar Aug 18 '16 05:08 ssrikanta

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 avatar Aug 18 '16 05:08 ssrikanta

@ssrikanta its by design impossible to access item on the master - what do you want to use it for?

RonnyPfannschmidt avatar Aug 18 '16 06:08 RonnyPfannschmidt

@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

RonnyPfannschmidt avatar Aug 18 '16 06:08 RonnyPfannschmidt

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

ssrikanta avatar Aug 18 '16 06:08 ssrikanta

so put the docstring into the report as a section

RonnyPfannschmidt avatar Aug 18 '16 07:08 RonnyPfannschmidt

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

ssrikanta avatar Aug 18 '16 07:08 ssrikanta

you put the section into the report in a hookwrapper for the makereport hook, then you can access it in the logreport hook

RonnyPfannschmidt avatar Aug 18 '16 07:08 RonnyPfannschmidt

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.

ssrikanta avatar Aug 18 '16 08:08 ssrikanta

all nodes (masters and slaves) run session_finish

RonnyPfannschmidt avatar Aug 18 '16 09:08 RonnyPfannschmidt