Skip to content

Call "--collect-only" programmatically #2039

Answered by nicoddemus
westhomas asked this question in General
Discussion options

You must be logged in to vote

I think the easiest way is by writing a plugin and invoking pytest.main() while enabling that plugin. This way you have the entire hook facility available to you.

# contents of runpytest.py
import sys
import pytest

class MyPlugin:

    def __init__(self):
        self.collected = []

    def pytest_collection_modifyitems(self, items):
        for item in items:
            self.collected.append(item.nodeid)


my_plugin = MyPlugin()
directory = sys.argv[1]
pytest.main(['--collect-only', directory], plugins=[my_plugin])

for nodeid in my_plugin.collected:
    print(nodeid)

Running python runpytest.py test_foo.py prints:

============================= test session starts ====================…

Replies: 5 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@yangyunguo
Comment options

Answer selected by nicoddemus
Comment options

westhomas
Nov 4, 2016
Collaborator Author

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@RonnyPfannschmidt
Comment options

@elrandira
Comment options

@RonnyPfannschmidt
Comment options

@elrandira
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants
Converted from issue

This discussion was converted from issue #2039 on September 26, 2020 15:30.