首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测量python线程的覆盖率

测量python线程的覆盖率
EN

Stack Overflow用户
提问于 2022-02-07 14:58:44
回答 1查看 328关注 0票数 0

我试图使用使用python threading模块创建的线程的代码来度量代码覆盖率。我正在使用coverage来测量覆盖率。但是,我无法获得在线程中运行的代码来进行度量。我试着跟踪关于覆盖文档的建议,以度量子进程中的覆盖率,但没有运气。

下面是一个最小的示例文件untitled.py

代码语言:javascript
复制
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()
代码语言:javascript
复制
> 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上运行,Python3.9.0,coverage 6.2。该目录包含以下.coveragerc文件:

代码语言:javascript
复制
# .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__.:

我非常感谢任何建议!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-07 18:06:05

您需要指定“线程”作为并发设置的一部分:

代码语言:javascript
复制
concurrency = multiprocessing,thread

我不确定你需要多处理。您的示例程序不使用它,也许您真正的程序也不使用它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71020663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档