我目前正在尝试获取python文件中所有测试方法的第一行和最后一行。你知道吗
例如:
文件测试_电梯.py测试文件电梯.py. 你知道吗
测试_电梯.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
...
我现在想得到test中每个测试方法的第一行和最后一行的编号_电梯.py,例如:
向上测试,7,10
向下测试,12,18
我已经试过使用确认测试.py,但没有成功。也许我忽略了什么? 解决方案不一定必须是python。如果有人知道如何通过解析文件来解决这个问题,我可以接受。你知道吗
Inspect在这方面很有效:
输出:
您可以使用
inspect
模块来完成此任务或者,在没有任何附加模块(但有一些Python内部构件)的情况下:
这样就有了:函数从第1行(
thing.__code__.co_firstlineno
)到第4行。你知道吗dis
模块证明了这一点(第一列中的数字是行号):注意最后一个数字是4,这是函数最后一行的数字。你知道吗
更多关于
co_lnotab
结构的信息可以在here和here中找到。你知道吗测试程序
输出:
相关问题 更多 >
编程相关推荐