使用python包装的c ++ SWIG

时间:2017-08-29 08:46:28

标签: python c++ unit-testing swig

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

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

当我到达c ++代码中的std :: cin时,Mocking 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()

1 个答案:

答案 0 :(得分:1)

花了一些时间,花了一个小时就犯了一个愚蠢的错误:

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

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

详细信息

MockTest类(我们正在尝试mockInput方法返回值patch)在模块MockTest中定义( !!!即 MockTest.py !!! )由 Swig 自动生成(我的情况下的命令是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 ++ (其名称为{{1}它由本机模块导出:MockTest_mockInput - 本地名称是 MocTest_wrap.cpp 中定义的_MockTest。当然,由于_wrap_MockTest_mockInput调用getInputMockTest_getInput patch Python 包装器)没有效果(就像你要修改它一样)让我们说:mockInput)。

以下是我准备好的一些示例代码,以便更好地说明行为(正如我在评论中提到的,我使用 Python 3.5.4 )。请忽略最后3行代码和输出,直到您阅读下一段:

return "123"

输出:

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 .cpp )但是MockTest::mockInput(来自 MocTest_wrap.cpp )。

下面是_wrap_MockTest_mockInput C ++ 代码与 Python 之间所有层的表格(对于mockInput它完全是相同):

mockInput layers

getInput调用getInput第0层)时,mockInput应该发生在哪里,但不幸的是,这不可用于patch 的Python

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