Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamf/aaeb8971b8304a24fe034d5ac4710f09 to your computer and use it in GitHub Desktop.
Save adamf/aaeb8971b8304a24fe034d5ac4710f09 to your computer and use it in GitHub Desktop.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/afletcher/thread/tests/test_tt.py", line 30, in test_timer_thread
tt.timer_thread.assert_has_calls(master_calls)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/mock.py", line 824, in assert_has_calls
) from cause
AssertionError: Calls not found.
Expected: [call(1), call(2), call(3), call(4), call(5), call(6), call(7), call(8), call(9)]
Actual: [call.__bool__(),
call(1),
call.__bool__(),
call(2),
call.__bool__(),
call(3),
call.__bool__(),
call(4),
call.__bool__(),
call(5),
call.__bool__(),
call(6),
call.__bool__(),
call(7),
call.__bool__(),
call(8),
call.__bool__(),
call(9)]
import tt
from importlib import reload
import unittest
from unittest.mock import patch, MagicMock, call
class TestTT(unittest.TestCase):
def setUp(self):
reload(tt)
def test_main(self):
tt.master_thread = MagicMock()
master_calls = [
call(),
]
tt.main()
tt.master_thread.assert_has_calls(master_calls)
def test_timer_thread(self):
tt.timer_thread = MagicMock()
master_calls = [
call(1),
call(2),
call(3),
call(4),
call(5),
call(6),
call(7),
call(8),
call(9),
]
tt.main()
tt.timer_thread.assert_has_calls(master_calls)
from threading import Thread
from time import sleep
import sys
def timer_thread(t):
for i in range(1, 10):
sleep(1)
print(t, i)
def master_thread():
ts = []
for t in range(1,10):
ts.append(Thread(target=timer_thread, args=(t,)))
for t in ts:
t.start()
for t in ts:
t.join()
def main():
print("Hello")
m = Thread(target=master_thread)
m.start()
m.join()
print("world")
if __name__ == '__main__':
sys.exit(main())
@adamf
Copy link
Author

adamf commented Oct 11, 2016

python3 -m unittest discover test_tt.py

Why does the test of timer_thread fail? I asked this question to some friends and vishnubob and jwm both pointed out that it's probably because Thread is testing if the target callable is valid, which results in seeing extra calls to the mock'd object's __bool__() - and this is indeed true, according to the python source. Mystery solved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment