# -.- coding:utf-8 -.-
import unittest
import threading
class TestCurrentThread(unittest.TestCase):
def test_main_thread(self):
current_thread = threading.current_thread()
self.assertEqual(current_thread.name, "MainThread")
def test_other_thread(self):
result = []
def other_thread():
current_thread = threading.current_thread()
result.append(current_thread)
t = threading.Thread(target=other_thread, args=())
t.start()
t.join()
self.assertTrue(result[0].name.startswith("Thread-"))
return result[0]
def test_main_thread_is_not_other_thread(self):
current_thread = threading.current_thread()
other_thread = self.test_other_thread()
self.assertEqual(current_thread.name, "MainThread")
self.assertTrue(other_thread.name.startswith("Thread-"))
self.assertNotEqual(current_thread.ident, other_thread.ident)
运行: tests/main.py
import unittest
TEST_MODULE = [
"ln_threading.tests.current_thread",
]
if __name__ == '__main__':
suite = unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULE)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
网友评论