使用 Python 单元测试 C 代码的最简单方法

发布于 2024-08-26 02:29:17 字数 394 浏览 1 评论 0原文

我有一堆 C 代码,我想使用 Python 的单元测试库(在 Windows 中)对其进行单元测试,但我正在尝试找出连接 C 代码的最佳方法,以便 Python 可以执行它(并且返回结果)。有人有以最简单的方式做到这一点的经验吗?

一些想法包括:

  • 使用 Python API 将代码包装为 Python C 扩展
  • 使用 SWIG 包装 C 代码
  • 将 DLL 包装器添加到 C 代码并使用 ctypes 将其加载到 Python
  • 将小型 XML-RPC 服务器添加到 c 代码并使用 xmlrpclib 调用它(是的,我知道这似乎有点遥远!)

有没有规范的方法可以做到这一点?我将使用不同的 C 模块做很多这样的事情,所以我想找到一种最省力的方法。

I've got a pile of C code that I'd like to unit test using Python's unittest library (in Windows), but I'm trying to work out the best way of interfacing the C code so that Python can execute it (and get the results back). Does anybody have any experience in the easiest way to do it?

Some ideas include:

  • Wrapping the code as a Python C extension using the Python API
  • Wrap the C code using SWIG
  • Add a DLL wrapper to the C code and load it into Python using ctypes
  • Add a small XML-RPC server to the c-code and call it using xmlrpclib (yes, I know this seems a bit far-out!)

Is there a canonical way of doing this? I'm going to be doing this quite a lot, with different C modules, so I'd like to find a way which is least effort.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏佲洛 2024-09-02 02:29:17

使用 ctypes 将是我的第一直觉,但我必须承认,如果我要测试一开始不会与 Python 连接的 C 代码,我只会使用 检查Check 具有强大的优势,能够正确报告出现段错误的测试用例。这是因为它在单独的进程中运行每个测试用例。

现在,如果您无论如何都要为 C 代码创建一个 python 包装器,我会简单地对该包装器进行单元测试。除了上面列出的方法之外,您还可以使用 Cython 使用类似 python 的代码编写这样的包装器然后从 Python 对其进行单元测试。

Using ctypes would be my first instinct, though I must admit that if I was testing C code that was not going to be interfaced from Python in the first place, I would just use check. Check has the strong advantage of being able to properly report test cases that segfault. This is because it runs each test case in a separate process.

Now if you are going to create a python wrapper for the C code anyway, I would simply unittest the wrapper. In addition to the methods you listed above, you could also use Cython to write such a wrapper with python-like code and then unittest it from Python.

攒眉千度 2024-09-02 02:29:17

我认为确切的解决方案取决于您的代码。并非所有库都适合包装为 DLL。如果您是这样,那么 ctypes 无疑是最简单的方法 - 只需从您的库中创建一个 DLL,然后使用 ctypes 对其进行测试即可。额外的好处是,您现在可以方便地将库包装为独立的 DLL,这有助于解耦您的应用程序。

然而,有时,C 代码和测试 Python 代码之间需要更彻底的交互。然后,最好将其作为一个扩展,SWIG 是一个非常好的工具,它可以自动化处理流程中您会觉得无聊的大部分事情。

I think that the exact solution depends on your code. Not all libraries are easily suitable for wrapping as a DLL. If your is, then ctypes is certainly the easiest way - just make a DLL out of your library and then test it with ctypes. An added bonus is that you now have your library conveniently wrapped as a standalone DLL which helps to decouple your application.

Sometimes, however, a more thorough interaction will be required between your C code and the testing Python code. Then, it's probably best to hook it as an extension, for which SWIG is a pretty good tool that will automate away most things you'll find boring about the process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文