0

I have a function, which I want to test, but spawns a thread, which can live long enough to wait its end. How to not wait the thread's end in this situation? I use django's unittests, but it is not the point, I guess. Example below:

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)

So, after launching the tests I have to wait until long_fun's thread ends it's execution, but I want the thread to end after the assertion happens. Is there a way to mock it or something?

Ivan
  • 316
  • 3
  • 15
  • why do you need the `long_func()` if it doesn't matter for the test to be correct? – Alexander Riedel Dec 21 '20 at 09:40
  • In my case the real function does something, and in the end it starts something long, I need to assert only the first part, what comes before the long part. Now I think, is it possible to separate in into two functions... – Ivan Dec 21 '20 at 10:40
  • yeah, you should separate the two parts i guess... – Alexander Riedel Dec 21 '20 at 13:43

0 Answers0