简体   繁体   English

具有高度相关功能的Qt中的单元测试

[英]Unit Testing in Qt with Highly Dependent Functions

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

How can I supply a block box in this situation? 在这种情况下如何提供积木盒?

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. 然后,这将使用您自己的那些方法的实现,并且您不需要具有需要服务器访问权限的正确实现。

See for example: 参见例如:

for more details. 更多细节。

PS. 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. 这种链接器行为的优点是,您不需要像google模拟框架那样先将类声明为接口。

You need to use mocking. 您需要使用模拟。 Google have a C++ mocking framework . Google有一个C ++模拟框架 You will also need to re-design your code to use interfaces in place of socket code, etc. which are replaced with mock objects when you run your tests. 您还需要重新设计代码,以使用接口代替套接字代码等,这些接口在运行测试时将被模拟对象替代。

Take a look at QTestLib . 看看QTestLib This is the unit testing framework that comes with Qt. 这是Qt随附的单元测试框架。 Qt allows you to simulate events like mouse click and keyboard events (signals) as well as to snoop on events (signals) generated by your application. Qt允许您模拟诸如鼠标单击和键盘事件(信号)之类的事件,以及监听由应用程序生成的事件(信号)。

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

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