Below text is not reducted for grammer.
Rationale:
In todays Software Development efforts if you are developing a long term software project it is essential that you give HIGH importance to Test Driven Development (TDD) approach. I cannot think of any Software Engineer that can develop and maintain the modules that he is responsible without writing white-box tests such as Unit and Integration Tests.
Software Engineers who resists to TDD approach are complaining mainly about tight software schedules and yet they dont really know the value of re-running tests before commiting changes to modules that they are responsible for.
Imagine in a tight schedule environment you are forced to add a new feature to the functionality you have implemented before. How would you be confident about the new implementation without re-testing the old functionality and make sure that nothing has broken. Etc...
Anyway I will mention about how googletest and googlemock eased my TDD approaches. Before meeting these testing frameworks I was using cppunit and my Fake Object implementations.
Drawbacks and struggles of cppunit
- You have to declare your test functions in .h implement them in .cpp and register them to the framework.
- I do not know of any mock framework that goes well with this test framework.
Drawbacks and struggles of Fake Object approach.
- I had to implement fake classes that were implementing some interfaces that real objects have implemented.
- I had to implement function switches in order to set some expectations before running my test code.
- I was ignoring to test positive test cases since implementation of the fake objects were becoming to complex for me. Because, I had to fill in all the structures required in my impementation such as when returning a structure from with in the function or as output argument of the function. So I was not implementing my Fake Objects to handle the positive cases and I was leaving this to the my Integration Tests.
- Because of my above approach my TDD was as following: First I was writing Integration Tests as I was developing, and finally I was writing Unit Tests to test negative cases at the end.
Why googletest and googlemock?
After all my time consuming TDD approaches I was looking for a better solution. When I first reserved some time for testing Googles Test Frameworks, I have observed the below benefits in order to decide contiuning developing with them.
- Google Test framework Test Functions require no declaration in header file, and neither require registeration of those functions.
- The console output legend is easy to understand with coloured texts etc..
- The Google Mock framework is just simply precompilation of expectations so I don't have to implement any Fake classes. You just declare the MOCK macros of your member functions that you are goint to mock. Below is an example
class MockWifiServiceApi : public WifiServiceImplementor{
public:
MockWifiServiceApi(ServiceManipulationInterfaceSharedPtr serviceApi):WifiServiceImplementor(serviceApi){};
MOCK_METHOD3(startService, ServiceReturnCodes(const QString& serviceName,
int timeOut,
bool * isStarted));
MOCK_METHOD3(stopService, ServiceReturnCodes(const QString& serviceName,
int timeOut,
bool * isStopped));
MOCK_METHOD2(isServiceExists, ServiceReturnCodes(const QString& serviceName,
bool * exists));
MOCK_METHOD2(isServiceRunning, ServiceReturnCodes(const QString& serviceName,
bool * running));
MOCK_METHOD1(locateService, ServiceReturnCodes(QString * serviceName));
};
- They also provide phyton scripts in order you to generate above header declaration automatically. Which is very nice.
- You can mock output arguments of a function in a handy way. And also provide you enough tricks for diffucult mocking problems such as arguments that dont have copy constructor or assignment operator defined.
- Finally it is very easy to learn and have a very good online wiki source.
Conclusion:
Googles Test Frameworks increased my productivity and helped me very much on enhancing the quality of my codes. Thanks to the Google Team for their significant contribution on open source development.