[英]Mocking input using python wrapped c++ SWIG
I'm new to swig and python unittesting. 我是swig和python单元测试的新手。
Here's what I'm trying to do. 这就是我想要做的。 I have a c++ function that requires user input.
我有一个需要用户输入的c ++函数。 The C++ code is wrapped into python code using SWIG.
使用SWIG将C ++代码封装到python代码中。 I'm trying to use pythons unittest module to mock the input.
我正在尝试使用pythons unittest模块来模拟输入。 Ive tried mocking builtins.input and writing my own basic function in c++ that just returns a string and mocking that.
我曾尝试模拟builtins.input并在c ++中编写我自己的基本函数,只返回一个字符串并模拟它。
Mocking builtins.input still hangs when I get to the std::cin in the c++ code. 当我到达c ++代码中的std :: cin时,模拟builtins.input仍然挂起。 and mocking the function that returns a string, doesnt return the mocked return_value.
并且模拟返回字符串的函数,并不返回模拟的return_value。
My guess is for some reason I cant mock the return value of the function because its really c++ code and not true python. 我的猜测是出于某些原因我无法模拟函数的返回值,因为它真的是c ++代码而不是真正的python。
Here's some example code I'm working with: 这是我正在使用的一些示例代码:
c++ I can include the header file is needed, but its really simple. c ++我可以包含头文件是必需的,但它真的很简单。
#include "MockTest.h"
#include <iostream>
#include <string>
MockTest::MockTest() {}
std::string MockTest::getInput()
{
std::string name;
std::cout << "Enter you name" << std::endl;
name = this->mockInput("hi"); //std::cin >> name;
std::string toReturn = "Hello " + name + " person";
return toReturn;
}
std::string MockTest::mockInput(std::string input)
{
return input;
}
swig interface file: swig接口文件:
%module MockTest
%include "std_string.i"
%include "std_vector.i"
%include "std_iostream.i"
%{
#include "MockTest.h"
%}
%include <MockTest.h>
python test script python测试脚本
from unittest.mock import patch
from unittest import TestCase
import unittest
import MockTest
class Test(TestCase):
@patch('builtins.input', return_value="Will")
def test_Mocktest(self, input):
self.assertEqual(MockTest.MockTest().getInput(), 'Hello Will person')
@patch('MockTest.MockTest.mockInput', return_value="Will")
def test_Mocktest2(self, mockInput):
self.assertEqual(MockTest.MockTest().getInput(), 'Hello Will person')
if __name__ == '__main__':
unittest.main()
After spending some time out of which ~ one hour on a stupid mistake: 花了一些时间在一个愚蠢的错误上花了一个小时:
public:
before class members in MockTest.h ... X:(((( public:
在MockTest.h中的类成员之前...... X :(((( and some more time investigating the real problem, I finally reached a conclusion: the problem is not because of the C++ language (at least not directly), but it lies in the intermediary layer(s) created by Swig . 还有一些时间调查真正的问题,我终于得出一个结论:问题不是因为C ++语言(至少不是直接),而是存在于Swig创建的中间层 。
The MockTest
class (whose mockInput
method return value we are trying to patch
) is defined in module MockTest
( !!! that is MockTest.py !!! ) which is automatically generated by Swig (the command in my case was swig -o MockTest_wrap.cpp -python -c++ MockTest.i
). 该
MockTest
类(其mockInput
方法的返回值,我们正在努力patch
)在模块定义MockTest
(!!!这是MockTest.py !!!),这是由痛饮 (在我的情况下,命令自动生成是swig -o MockTest_wrap.cpp -python -c++ MockTest.i
)。 Here's its definition: 这是它的定义:
class MockTest(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, MockTest, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, MockTest, name)
__repr__ = _swig_repr
def __init__(self):
this = _MockTest.new_MockTest()
try:
self.this.append(this)
except __builtin__.Exception:
self.this = this
__swig_destroy__ = _MockTest.delete_MockTest
__del__ = lambda self: None
def getInput(self):
return _MockTest.MockTest_getInput(self)
def mockInput(self, input):
return _MockTest.MockTest_mockInput(self, input)
As you probably guessed, this mockInput
is being patch
ed and not the one from C++ (whose name is MockTest_mockInput
and it's exported by the native module: _MockTest
- the native name is _wrap_MockTest_mockInput
defined in MocTest_wrap.cpp ). 正如您可能猜到的那样, 这个
mockInput
正在patch
而不是C ++中的那个(其名称为MockTest_mockInput
,它由本机模块导出: _MockTest
- 本机名称是在_wrap_MockTest_mockInput
定义的_wrap_MockTest_mockInput )。 Of course, since getInput
calls MockTest_getInput
, patch
ing mockInput
(the Python wrapper) has no effect (just like if you would modify its body to let's say: return "123"
). 当然,由于
getInput
调用MockTest_getInput
, patch
mockInput
( Python包装器)没有任何效果(就像你修改它的主体让我们说: return "123"
)。
Here's some sample code that I prepared to better illustrate the behavior (as I mentioned in my comment, I'm using Python 3.5.4 ). 这是我准备好的一些示例代码,以便更好地说明行为(正如我在评论中提到的,我使用的是Python 3.5.4 )。 Please ignore the last 3 lines of code and output til you read the next paragraph:
请忽略最后3行代码和输出,直到您阅读下一段:
from unittest.mock import patch
import MockTest
if __name__ == "__main__":
return_value = "value overridden by `patch`"
mock_input_arg = "argument passed to `MockTest.mockInput`"
mock_test = MockTest.MockTest()
with patch("MockTest.MockTest.mockInput", return_value=return_value):
print("`mock_test.getInput()` returned: \"{}\"".format(mock_test.getInput()))
print("`mock_test.mockInput(\"{}\")` returned: \"{}\"".format(mock_input_arg, mock_test.mockInput(mock_input_arg)))
print("\nDon't mind the following output for now...\n") # SAME THING about the following code
with patch("MockTest._MockTest.MockTest_mockInput", return_value=return_value):
print("`mock_test.getInput()` returned: \"{}\"".format(mock_test.getInput()))
print("`mock_test.mockInput(\"{}\")` returned: \"{}\"".format(mock_input_arg, mock_test.mockInput(mock_input_arg)))
And the output: 并输出:
c:\\Work\\Dev\\StackOverflow\\q45934545>"c:\\Install\\x64\\Python\\Python\\3.5\\python.exe" dummy.py Enter you name `mock_test.getInput()` returned: "Hello hi person" `mock_test.mockInput("argument passed to `MockTest.mockInput`")` returned: "value overridden by `patch`" Don't mind the following output for now... Enter you name: `mock_test.getInput()` returned: "Hello hi person" `mock_test.mockInput("argument passed to `MockTest.mockInput`")` returned: "value overridden by `patch`"
Then I went further trying to patch
MockTest._MockTest.MockTest_mockInput
, but I got the same output, because I didn't patch
MockTest::mockInput
(from MockTest.cpp ) but _wrap_MockTest_mockInput
(from MocTest_wrap.cpp ). 然后我进一步尝试
patch
MockTest._MockTest.MockTest_mockInput
,但我得到了相同的输出,因为我没有patch
MockTest::mockInput
(来自MockTest.cpp )而是_wrap_MockTest_mockInput
(来自MocTest_wrap.cpp )。
Below is a table with all the layers between C++ code and Python for mockInput
(for getInput
it's exactly the same): 下面是一个表格,其中包含C ++代码和Python之间用于
mockInput
所有层(对于getInput
它完全相同):
As getInput
calls mockInput
( layer 0 ), there is where the patch
should happen, but unfortunately, that's not available to Python . 当
getInput
调用mockInput
( 第0层 )时, patch
应该发生在哪里,但遗憾的是, Python无法使用。
The 1 st solution that comes into my mind, is patch
ing getInput
(layer 1) directly (if it's used in many places). 我想到的第一个解决方案是直接
patch
getInput
(第1层)(如果在很多地方使用它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.