具有高度相关功能的Qt中的单元测试 [英] Unit Testing in Qt with Highly Dependent Functions

查看:96
本文介绍了具有高度相关功能的Qt中的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试的新手。我想在Qt中编写单元测试代码,但是我的功能(登录,请求等)在很大程度上取决于其他资源,例如服务器。

I'm new to unit testing. I want to code unit tests in Qt, but my functions(login, request etc...) heavily depend on other resources such as a server.

我该如何提供

您知道吗,我可以检查类似情况下的单元测试类的任何开源项目吗?

Do you know any open source project which I can examine unit test classes for similar situations?

推荐答案

有一个可以使用的链接器技巧。您知道您的代码使用哪些方法和类。获取这些类的头文件和声明,并对每个类进行小的实现,以返回所需的值。然后编译它们并以正确的顺序链接。然后,这将使用您自己的那些方法的实现,并且您不需要具有需要服务器访问权限的正确实现。

There's a linker trick that you can use. You know which methods and classes your code uses. Get a header files and declarations of those classes and make a small implementation of each that returns values you would like. Then compile those and link in right order. This will then use your own implementation of those methods and you don't need to have the right implementations that require server access.

例如参见

  • http://en.wikipedia.org/wiki/Mock_object
  • http://martinfowler.com/articles/mocksArentStubs.html

了解更多详情。

PS。这种链接器行为的优点是,您无需首先将类声明为接口,例如google模拟框架需要。

PS. Advantage of this linker behaviour is that you don't need to declare your classes as interface first like for example, google mocking framework requires.

这篇关于具有高度相关功能的Qt中的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆