Last active
October 11, 2016 16:48
-
-
Save adamf/aaeb8971b8304a24fe034d5ac4710f09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---------------------------------------------------------------------- | |
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 becauseThread
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!