Python pytest Mock中的模拟线程调用
我有一个简单的代码库,如下所示:Python pytest Mock中的模拟线程调用,python,multithreading,unit-testing,pytest-mock,Python,Multithreading,Unit Testing,Pytest Mock,我有一个简单的代码库,如下所示: import threading import time class Demo: def __init__(self): self._run_thread = False def start(self): if not self._run_thread: print("Thread has not run") threading.Thread(target=sel
import threading
import time
class Demo:
def __init__(self):
self._run_thread = False
def start(self):
if not self._run_thread:
print("Thread has not run")
threading.Thread(target=self.counter, args=(20, )).start()
return 20
else:
print("Thread has run")
return None
def counter(self, value):
self._run_thread = True
for i in range(value):
print(f"Inside counter with value {i}")
time.sleep(3)
if __name__ == "__main__":
d = Demo()
d.start()
d.start()
第一次调用start
函数时,线程将运行,下一次调用时不会运行,因为设置了标志(_run_thread)
代码本身运行得非常好
但是,当我尝试对这段代码运行测试时,它并没有像预期的那样工作。下面是我的测试代码:
import app
def dummy_counter(self, value):
self._run_thread = True
def test_app_start_function_runs_well(mocker):
d = app.Demo()
mocker.patch.object(
d,
"counter",
dummy_counter
)
d.start()
result = d.start()
assert result is None
因此,由于我不希望循环实际运行,我只想模拟计数器
函数,然后在其内部设置标志,以便实际线程不会运行
我看到下面的错误
(roughwork) ➜ threads git:(master) ✗ pytest
============================================================ test session starts ============================================================
platform linux -- Python 3.7.6, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /home/subhayan/Codes/Test/Python-remote/threads
plugins: mock-3.1.1
collected 1 item
test_app.py Exception in thread Thread-2:
Traceback (most recent call last):
File "/home/subhayan/anaconda3/envs/roughwork/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/home/subhayan/anaconda3/envs/roughwork/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
TypeError: dummy_counter() missing 1 required positional argument: 'value'
F [100%]
================================================================= FAILURES ==================================================================
____________________________________________________________ test_app_runs_well _____________________________________________________________
mocker = <pytest_mock.plugin.MockFixture object at 0x7f922c05ee90>
def test_app_runs_well(mocker):
d = app.Demo()
mocker.patch.object(
d,
"counter",
dummy_counter
)
d.start()
result = d.start()
> assert result is None
E assert 20 is None
test_app.py:15: AssertionError
----------------------------------------------------------- Captured stdout call ------------------------------------------------------------
Thread has not run
Thread has not run
----------------------------------------------------------- Captured stderr call ------------------------------------------------------------
Exception in thread Thread-1:
Traceback (most recent call last):
File "/home/subhayan/anaconda3/envs/roughwork/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/home/subhayan/anaconda3/envs/roughwork/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
TypeError: dummy_counter() missing 1 required positional argument: 'value'
========================================================== short test summary info ==========================================================
FAILED test_app.py::test_app_runs_well - assert 20 is None
(粗活)➜ 线程git:(主线程)✗ 皮特斯特
=======================================================================================测试会话开始============================================================
平台linux——Python 3.7.6、pytest-5.4.3、py-1.8.1、pluggy-0.13.1
rootdir:/home/subhayan/code/Test/Python remote/threads
插件:mock-3.1.1
收集1项
线程2中的test_app.py异常:
回溯(最近一次呼叫最后一次):
文件“/home/subhayan/anaconda3/envs/rawwork/lib/python3.7/threading.py”,第926行,在内部引导中
self.run()
文件“/home/subhayan/anaconda3/envs/rawwork/lib/python3.7/threading.py”,第870行,运行中
自我目标(*自我参数,**自我参数)
TypeError:dummy_counter()缺少1个必需的位置参数:“value”
F[100%]
======================================================================================================故障==================================================================
____________________________________________________________测试应用程序运行良好_____________________________________________________________
嘲弄者=
def测试应用程序运行良好(模拟程序):
d=app.Demo()
mocker.patch.object(
D
“柜台”,
虚拟计数器
)
d、 开始()
结果=d.开始()
>断言结果为无
E断言20是零
测试应用程序py:15:AssertionError
-----------------------------------------------------------捕获的stdout调用------------------------------------------------------------
线程尚未运行
线程尚未运行
-----------------------------------------------------------捕获的stderr调用------------------------------------------------------------
线程1中的异常:
回溯(最近一次呼叫最后一次):
文件“/home/subhayan/anaconda3/envs/rawwork/lib/python3.7/threading.py”,第926行,在内部引导中
self.run()
文件“/home/subhayan/anaconda3/envs/rawwork/lib/python3.7/threading.py”,第870行,运行中
自我目标(*自我参数,**自我参数)
TypeError:dummy_counter()缺少1个必需的位置参数:“value”
=================================================================================================简短测试摘要信息==========================================================
失败的测试应用程序。py::测试应用程序运行良好-断言20为无
我不知道哪里出了问题。如果有任何线索,我将不胜感激