Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of all pytest node ids using python

Do you know if there is a way to collect all pytest node ids (as presented here) using the pytest python API ?

I have found the --collect-only parameter of pytest, but I can't figure out how to get the output using python ?

Thanks in advance !

like image 474
Kahsius Avatar asked May 27 '26 01:05

Kahsius


1 Answers

If you want to access nodeids programmatically, best is to write a small plugin that will store them on test collection. Example:

import pytest


class NodeidsCollector:
    def pytest_collection_modifyitems(self, items):
        self.nodeids = [item.nodeid for item in items]


def main():
    collector = NodeidsCollector()
    pytest.main(['--collect-only'], plugins=[collector])
    # use collector.nodeids now

If you want to avoid pytest output to the terminal, disable the terminal plugin in addition:

pytest.main(['--collect-only', '-pno:terminal'], plugins=[collector])
like image 51
hoefling Avatar answered May 28 '26 15:05

hoefling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!