I’m trying to customize my test runs using Pytest and I’ve read about the pytest_collection_finish hook. I’m working on a Windows 10 machine with Python 3.9 and Pytest 6.2.4. I want to modify the test collection process to gather specific metadata about the tests. However, when I try to implement this hook, I run into an “AttributeError: ‘Config’ object has no attribute” error. I’ve checked my pytest.ini and other configuration files, but nothing seems amiss. What might be causing this, and how can I properly use pytest_collection_finish? Any insights into the typical pitfalls would be appreciated.
Encountering the issues with the pytest_collection_finish hook is something I’ve seen quite often. When first using it, I found it essential to go through its setup carefully.
The root cause of your problem might stem from how the hook integrates with the existing configuration and the test collection process. This hook allows you to access and modify the test collection, but you need to ensure that your implementation correctly interacts with pytest’s lifecycle. Often, the error you’re seeing about the Config object missing an attribute is due to incorrect assumptions about available objects at this stage.
Here is the snippet that worked for me:
def pytest_collection_finish(session):
print("Test collection completed.")
for item in session.items:
print(item.nodeid)
This code prints out each test’s unique identifier once collection is complete. Be sure to interact with session.items during this hook, not during pytest_collect_report or others, as this is where the items are finalized.
When implementing, avoid settings or config manipulations that aren’t valid at this collection stage. Double-check your pytest.ini and command-line arguments for compatibility. If you’re using plugins, make sure they’re updated as they might also influence test collection behavior or object attributes.
Finally, always read the Pytest documentation for the specific version you’re using to understand any version-specific changes or deprecated features.
Trying a different approach can often yield insights into why pytest_collection_finish isn’t working as expected. In my case, similar errors occurred until I changed how I handled session objects.
The problem might be less about the hook itself and more about what you’re trying to access within the session. It’s crucial to ensure the attributes you want to modify or print are initialized and accessible at the time pytest_collection_finish is called. Consider what is available at this lifecycle point, which differs slightly from the ordering in earlier hooks like pytest_collection_modifyitems.
Here is how I approached it:
def pytest_collection_finish(session):
metadata = []
for item in session.items:
metadata.append({'name': item.name, 'location': item.location})
print(metadata)
This example captures and prints metadata about each test after the collection phase is complete. Pay attention to the structure of the session object, as incorrect assumptions about its makeup can lead to errors.
Making sure you thoroughly understand each attribute’s life cycle helps circumvent these issues. Reviewing the official Pytest lifecycle documentation helped me clear up many misconceptions.
The key is iterating over session.items only after full collection and gathering any data needed without attempting to alter immutable structures. Always tailor the hook logic to your specific use case and environment conditions.