python - 不要在测试函数中等待产生的线程
问题描述
我有一个函数,我想测试它,但会产生一个线程,它可以存活足够长的时间来等待它的结束。在这种情况下如何不等待线程结束?我使用 django 的单元测试,但我猜这不是重点。下面的例子:
import threading
from time import sleep
from unittest import TestCase
def long_fun():
threading.Thread(target=sleep, args=(60 * 60,)).start()
class TestLongFun(TestCase):
def test_long_fun(self):
long_fun()
self.assertTrue(1)
因此,在启动测试后,我必须等到long_fun
' 线程结束它的执行,但我希望线程在断言发生后结束。有没有办法模拟它或其他什么?
解决方案
推荐阅读
- python - pip 给出错误“pip 配置了需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用。” 在麦克
- css - flexbox 中的图像在 Firefox 中无法正确调整大小
- xml - 保持 xml 中的新行发送到 xslt
- azure-devops - Azure-devops:共享来自运行自己作业的不同代理的输出
- angular - 选择特定项目时将选择更改为文本框?
- sql-server - 将函数从 Oracle 转换为 SQL Server
- javascript - 从鼠标悬停回调中添加点击事件
- wso2esb - wso2 ei 6.5.0 注册表搜索不起作用
- php - 如何在 heroku 中安装 PHP 附魔和 pspell 扩展?
- python-3.7 - 无法在 Windows 上安装 locust