pytest-dev / pytest-forked

extracted --boxed from pytest-xdist to ensure backward compat
MIT License
67 stars 21 forks source link

xdist master cant see workers' workeroutput when using --forked #87

Open PatrikMosko opened 1 year ago

PatrikMosko commented 1 year ago

Hi fellas.

I am building a tests profiler for collecting various statistics before/during test cases execution. Since we use pytest-xdist, I decided its finally time to look into how new xdist hooks from newhooks.py work and try to get as much value out of them as possible, since doing shenanigans with locks and writing output to tmp files is pretty ugly.

I have finished my profiler MVP, and it was working nicely, until I tried to run it in our jenkins which uses --forked option. setup python-3.8.10 pytest-7.4.3 pytest-xdist-3.3.1 pytest-forked-1.6.0

ISSUE Turned out no stats are being displayed when I use this option. Inside pytest_testnodedown hook, where master node is collecting and aggregating collected statistics from all worker nodes, the node.workeroutput attribute is simply empty.

Is there some workaround for this, on how to properly send data from workers to master when using --forked? Am I just missing something?

Here are 2 most important hooks used for collecting and aggregating all the statistics.

    @pytest.hookimpl(hookwrapper=True, tryfirst=True)
    def pytest_sessionfinish(self):
        worker_id = get_xdist_worker_id(self)
        if worker_id != "master":
            # NOTE: Execnet can't serialize custom classes. Serialization has to be
            # implemented for everything we want to send in-between master and workers
            self.config.workeroutput[
                f"{worker_id}-stats"
            ] = self._profiling_stats.serialize()
        yield

    @pytest.hookimpl(trylast=True)
    def pytest_testnodedown(self, node):
        worker_id = node.workerinput["workerid"]
        node_stats = node.workeroutput[f"{worker_id}-stats"]
        node_stats = ProfilerNamespaceUnit.deserialize(node_stats)
        self._profiling_stats.merge_trees(node_stats)