在 python 个线程中测量覆盖率
Measuring coverage in python threads
我正在尝试使用使用 python threading
模块创建的线程的代码来测量代码覆盖率。
我正在使用 coverage
来衡量覆盖率。
但是,我无法在要测量的线程中获取 运行 的代码。我试过 suggestion on the coverage docs to measure coverage in subprocesses but 没有运气。
这是一个最小的示例文件 untitled.py
:
import time, threading
import numpy as np
def thread():
print(f'{threading.get_ident()}: started thread')
time.sleep(np.random.uniform(1, 10))
print(f'{threading.get_ident()}: done')
def run_threads():
threads = [threading.Thread(target=thread)]
for t in threads:
t.start()
print('started all threads')
for t in threads:
t.join()
print('all threads done')
if __name__ == '__main__':
run_threads()
> coverage run untitled.py
139952541644544: started thread
started all threads
139952541644544: done
all threads done
> coverage combine
Combined data file .coverage.248973.677959
> coverage report -m
Name Stmts Miss Cover Missing
-------------------------------------------
untitled.py 14 3 79% 6-8
-------------------------------------------
TOTAL 14 3 79%
>
如您所见,第 6-8 行(thread()
函数)已执行但未测量。
对于上下文,我 运行 在 linux 机器上,Python 3.9.0,coverage
6.2。该目录包含此 .coveragerc
文件:
# .coveragerc to control coverage.py
[run]
source = ./
parallel = True
concurrency = multiprocessing
[report]
# Regexes for lines to exclude from consideration
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover
# Don't complain about missing debug-only code:
def __repr__
if self\.debug
# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError
# Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:
非常感谢任何建议!!
您需要在并发设置中指定“线程”:
concurrency = multiprocessing,thread
我不确定您是否需要多处理。你的示例程序没有使用它,也许你的真实程序也没有。
我正在尝试使用使用 python threading
模块创建的线程的代码来测量代码覆盖率。
我正在使用 coverage
来衡量覆盖率。
但是,我无法在要测量的线程中获取 运行 的代码。我试过 suggestion on the coverage docs to measure coverage in subprocesses but 没有运气。
这是一个最小的示例文件 untitled.py
:
import time, threading
import numpy as np
def thread():
print(f'{threading.get_ident()}: started thread')
time.sleep(np.random.uniform(1, 10))
print(f'{threading.get_ident()}: done')
def run_threads():
threads = [threading.Thread(target=thread)]
for t in threads:
t.start()
print('started all threads')
for t in threads:
t.join()
print('all threads done')
if __name__ == '__main__':
run_threads()
> coverage run untitled.py
139952541644544: started thread
started all threads
139952541644544: done
all threads done
> coverage combine
Combined data file .coverage.248973.677959
> coverage report -m
Name Stmts Miss Cover Missing
-------------------------------------------
untitled.py 14 3 79% 6-8
-------------------------------------------
TOTAL 14 3 79%
>
如您所见,第 6-8 行(thread()
函数)已执行但未测量。
对于上下文,我 运行 在 linux 机器上,Python 3.9.0,coverage
6.2。该目录包含此 .coveragerc
文件:
# .coveragerc to control coverage.py
[run]
source = ./
parallel = True
concurrency = multiprocessing
[report]
# Regexes for lines to exclude from consideration
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover
# Don't complain about missing debug-only code:
def __repr__
if self\.debug
# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError
# Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:
非常感谢任何建议!!
您需要在并发设置中指定“线程”:
concurrency = multiprocessing,thread
我不确定您是否需要多处理。你的示例程序没有使用它,也许你的真实程序也没有。