views:

83

answers:

3

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?

+2  A: 

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.

Rob
A: 

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.

doron
I guess, there is no mocking in QTestLib.
metdos
+2  A: 

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

rasjani