开发者

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.

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:

  • 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜