繁体   English   中英

使用python包装的c ++ SWIG模拟输入

[英]Mocking input using python wrapped c++ SWIG

我是swig和python单元测试的新手。

这就是我想要做的。 我有一个需要用户输入的c ++函数。 使用SWIG将C ++代码封装到python代码中。 我正在尝试使用pythons unittest模块来模拟输入。 我曾尝试模拟builtins.input并在c ++中编写我自己的基本函数,只返回一个字符串并模拟它。

当我到达c ++代码中的std :: cin时,模拟builtins.input仍然挂起。 并且模拟返回字符串的函数,并不返回模拟的return_value。

我的猜测是出于某些原因我无法模拟函数的返回值,因为它真的是c ++代码而不是真正的python。

这是我正在使用的一些示例代码:

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接口文件:

%module MockTest
%include "std_string.i"
%include "std_vector.i"
%include "std_iostream.i"

%{
#include "MockTest.h"
%}
%include <MockTest.h>

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()

花了一些时间在一个愚蠢的错误上花了一个小时:

  • 生成的模块不完整的事实( 链接器在没有向其提供MockTest对象时没有抱怨未定义的符号)
  • 最后因为我忘了添加public:MockTest.h中的类成员之前...... X :((((

还有一些时间调查真正的问题,我终于得出一个结论:问题不是因为C ++语言(至少不是直接),而是存在Swig创建的中间层

细节

MockTest类(其mockInput方法的返回值,我们正在努力patch )在模块定义MockTest (!!!这是MockTest.py !!!),这是由痛饮 (在我的情况下,命令自动生成是swig -o MockTest_wrap.cpp -python -c++ MockTest.i )。 这是它的定义:

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)

正如您可能猜到的那样, 这个 mockInput正在patch而不是C ++中的那个(其名称为MockTest_mockInput ,它由本机模块导出: _MockTest - 本机名称是在_wrap_MockTest_mockInput定义的_wrap_MockTest_mockInput )。 当然,由于getInput调用MockTest_getInputpatch mockInputPython包装器)没有任何效果(就像你修改它的主体让我们说: return "123" )。

这是我准备好的一些示例代码,以便更好地说明行为(正如我在评论中提到的,我使用的是Python 3.5.4 )。 请忽略最后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)))

并输出:

 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`" 

然后我进一步尝试patch MockTest._MockTest.MockTest_mockInput ,但我得到了相同的输出,因为我没有patch MockTest::mockInput (来自MockTest.cpp )而是_wrap_MockTest_mockInput (来自MocTest_wrap.cpp )。

下面是一个表格,其中包含C ++代码和Python之间用于mockInput所有层(对于getInput它完全相同):

mockInput图层

getInput调用mockInput第0层 )时, patch应该发生在哪里,但遗憾的是, Python无法使用。

我想到的第一个解决方案是直接patch getInput (第1层)(如果在很多地方使用它)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM