Skip to content

Instantly share code, notes, and snippets.

@bonprosoft
Created September 20, 2019 17:29
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 bonprosoft/25de2daeea4b507c9bda3301c848ab87 to your computer and use it in GitHub Desktop.
Save bonprosoft/25de2daeea4b507c9bda3301c848ab87 to your computer and use it in GitHub Desktop.
mock.patchの検証
import threading
import time
from unittest import mock
class Hoge:
b = 10
a = Hoge()
def foo():
print(f'[Foo]before-patch: {a.b}')
with mock.patch('__main__.a.b', 100):
print(f'[Foo]in-patch1: {a.b}')
time.sleep(1)
print(f'[Foo]in-patch2: {a.b}')
print(f'[Foo]after-patch: {a.b}')
def bar():
for _ in range(10):
print(f'[Bar] {a.b}')
time.sleep(0.3)
t = threading.Thread(target=foo)
t.start()
bar()
t.join()
[Foo]before-patch: 10
[Bar] 10
[Foo]in-patch1: 100
[Bar] 100
[Bar] 100
[Bar] 100
[Foo]in-patch2: 100
[Foo]after-patch: 10
[Bar] 10
[Bar] 10
[Bar] 10
[Bar] 10
[Bar] 10
[Bar] 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment