Unit Testing in Qt with Highly Dependent Functions

牧云@^-^@ 提交于 2019-12-08 08:43:12

问题


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.

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?


回答1:


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:

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

for more details.

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.




回答2:


You need to use mocking. Google have a C++ mocking framework. 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.




回答3:


Take a look at QTestLib. This is the unit testing framework that comes with 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.



来源:https://stackoverflow.com/questions/3530889/unit-testing-in-qt-with-highly-dependent-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!