[Python] Using hunter to trace test calls misses certain invocations — how to capture more...

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I’m working on a project with the following goal: extract a self-contained set of Python modules and their corresponding test cases from a PyPI package, so that this extracted code (plus tests) can run independently.

    Here’s what I’m doing so far:


    1. I use the package’s own test scripts (usually named something like test_xxx.py) to invoke the code under test.


    2. I use hunter to dynamically track function calls (call/return) during the tests, in order to figure out which functions are actually being exercised.


    3. Based on the captured function calls, I extract the relevant source code and dependencies, hoping to form a minimal set of modules plus test code.

    The problem:

    While hunter is great for capturing function call/return events, it does not detect other types of usage, such as isinstance(A, SomeClass), class instantiations, or certain operator overloads. Consequently, a lot of necessary code isn’t recognized as “used,” so my extracted subset ends up being incomplete and fails to run.

    I would really appreciate any guidance on:

    • More comprehensive or flexible dynamic analysis: Are there alternative tools or techniques (beyond hunter and the basic sys.settrace / trace capabilities) that can capture a wider range of Python runtime events (e.g., class checks, object instantiations, etc.)?
    • Configuring hunter: Is there a way to configure hunter (or similar libraries) to pick up more than just function call/return events?
    • Other solutions: Are there recommended libraries, tools, or approaches (maybe coverage.py, modulegraph, or an import tracer) that work well for collecting all necessary code dependencies from test runs?

    Any advice on how to automate or streamline the process of extracting a minimal subset of modules and tests would be extremely helpful. Thank you in advance!

    Continue reading...

Compartilhe esta Página