首页 > 解决方案 > 不要在测试函数中等待产生的线程

问题描述

我有一个函数,我想测试它,但会产生一个线程,它可以存活足够长的时间来等待它的结束。在这种情况下如何不等待线程结束?我使用 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' 线程结束它的执行,但我希望线程在断言发生后结束。有没有办法模拟它或其他什么?

标签: pythonpython-3.xunit-testingpython-unittestdjango-unittest

解决方案


推荐阅读