[Python] How to get begining line number and end line number of a test method in python?

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

  1. Stack

    Stack Membro Participativo

    I'm currently trying to get the first and last line numbers of all test methods in a python file.

    For example:

    The file test_lift.py tests the file lift.py.

    test_lift.py

    1 import random
    2
    3 from src.lift import Lift
    4 from flaky import flaky
    5
    6
    7 def test_upwards():
    8 tester = Lift()
    9 tester.upwards()
    10 assert 1 == tester.curr_floor
    11
    12 def test_downwards():
    13 tester = Lift()
    14 tester.curr_floor = 1
    15 tester.downwards()
    16 assert 0 == tester.curr_floor
    17 tester.downwards()
    18 assert 0 == tester.curr_floor
    19
    ...


    I now want to get the first and last line number for each test method in test_lift.py, in this case for example:

    test_upwards, 7, 10

    test_downwards, 12, 18

    I've already tried using the conftest.py, but without any success. Maybe I'm overlooking something? The solution doesn't necessarily have to be in python. If anybody knows how to solve this problem by parsing the files I'm fine with that.

    Continue reading...

Compartilhe esta Página