Hi,

For pytest-testmon plugin I need to extract all node ids which exist in a
python file (after parametrization). Initially, I thought
pytest_collection_modifyitems used as a hookwrapper would have all the
node_ids in all the files (which would be ideal for me). It turns out if
you call pytest test_a.py::test_1 , pytest_collection_modifyitems only gets
test_a.py::test_1 (but no other nodes which might be in test_a.py).
pytest_pycollect_makemodule sounds like the pretty low-level hook which
should get all I need. However, it gets a hierarchy, which I have to
manually flatten. My gen_nodeids bellow looks like a code that definitely
lives somewhere in pytest. What is the optimal and most forward-compatible
way to get the list, please? Below is what I came up with so far (and it
seems to work).

I would be glad to get any comments or pointers.

Best,
Tibor
twitter: tibor_a

import pytest

pytest_plugins = "pytester"


def test_pytest_assumption(testdir):
    testdir.makepyfile(test_a="""
        class Test1():
            def test_1(self):
                pass
    """)

    class Plugin:
        @pytest.hookimpl(hookwrapper=True)
        def pytest_pycollect_makemodule(self, path, parent):
            def gen_nodeids(nodes):
                for node in nodes:
                    if isinstance(node, pytest.Function):
                        yield node.nodeid
                    else:
                        yield from gen_nodeids(node.collect())

            make_module_result = yield
            nodeids =
list(gen_nodeids(make_module_result.get_result().collect()))
            assert nodeids == ['test_a.py::Test1::test_1']

    result = testdir.runpytest_inprocess("--testmon-dev", plugins=[Plugin()])
    result.assert_outcomes(1, 0, 0)
_______________________________________________
pytest-dev mailing list
pytest-dev@python.org
https://mail.python.org/mailman/listinfo/pytest-dev

Reply via email to