Call "--collect-only" programmatically #2039
-
I need to get a list of tests that py.test can see in my code. I can see Lines 138 to 139 in 8639bf7 But I'm not sure how to get a session or the config necessary to create a session. What's the sequence of commands to be able to call the |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
I think the easiest way is by writing a plugin and invoking # 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
If you don't want any pytest output, you can disable the pytest.main(['--collect-only', '-p', 'no:terminal', directory], plugins=[my_plugin])
|
Beta Was this translation helpful? Give feedback.
-
Perfect! Thanks! |
Beta Was this translation helpful? Give feedback.
-
Is this still considered the way to go? |
Beta Was this translation helpful? Give feedback.
-
for me, MyPlugin.pytest_collection_modifyitems() was never called by Pytest I had to replace it with a MyPlugin.pytest_collection_finish method. It worked very well :) |
Beta Was this translation helpful? Give feedback.
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.Running
python runpytest.py test_foo.py
prints: