How do you implement unit-testing in large scale C

2019-01-30 18:27发布

I believe strongly in using unit-tests as part of building large multi-platform applications. We currently are planning on having our unit-tests within a separate project. This has the benefit of keeping our code base clean. I think, however, that this would separate the test code from the implementation of the unit. What do you think of this approach and are there any tools like JUnit for c++ applications?

9条回答
Bombasti
2楼-- · 2019-01-30 18:40

Cppunit is a direct equivalent of Junit for C++ applications http://cppunit.sourceforge.net/cppunit-wiki

Personally, I created the unit tests in a different project, and created a separate build configuration which built all the unit tests and dependent source code. In some cases I wanted to test private member functionss of a class so I made the Test class a friend class to the object to be tested, but hid the friend declarations when building in "non-test" configurations through preprocessor declarations.

I ended up doing these coding gymnastics as I was integrating tests into legacy code however. If you are starting out with the purpose of unit testing a better design may be simple.

查看更多
来,给爷笑一个
3楼-- · 2019-01-30 18:43

You can create a unit test project for each library in your source tree in a subdirectory of that library. You end up with a test driver application for each library, which makes it easier to run a single suite of tests. By putting them in a subdirectory, it keeps your code base clean, but also keeps the tests close to the code.

Scripts can easily be written to run all of the test suites in your source tree and collect the results.

I've been using a customized version of the original CppUnit for years with great success, but there are other alternatives now. GoogleTest looks interesting.

查看更多
做自己的国王
4楼-- · 2019-01-30 18:44

There are many Test Unit frameforks for C++. CppUnit is certainly not the one I would choose (at least in its stable version 1.x, as it lacks many tests, and requires a lot of redundant lines of codes). So far, my preferred framework is CxxTest, and I plan on evaluating Fructose some day.

Any way, there are a few "papers" that evaluate C++ TU frameworks :

查看更多
Evening l夕情丶
5楼-- · 2019-01-30 18:44

You should separate your base code to a shared (dynamic) library and then write the major part of your unit tests for this library.

Two years ago (2008) I have been involved in large LSB Infrastructure project deployed by The Linux Foundation. One of the aims of this project was to write unit tests for 40.000 functions from the Linux core libraries. In the context of this project we have created the AZOV technology and the basic tool named API Sanity Autotest in order to automatically generate all the tests. You may try to use this tool to generate unit tests for your base library (ies).

查看更多
狗以群分
6楼-- · 2019-01-30 18:45

I think your on the right path with unit testing and its a great plan to improve reliability of your product.

Though unit testing is not going to solve all your problems when converting your application to different platforms or even different operating systems. The reason for this, is the process unit testings goes through to uncover bugs in your application. It just simply throws as many inputs imaginable into your system and waits for a result on the other end. Its like getting a monkey to constantly pound at the keyboard and observing the results(Beta testers).

To take it to the next step, with good unit testing you need to focus on your internal design of your application. The best approach i found was to use a design pattern or design process called "contract programming" or "Design by contract". The other book that is very helpful for building reliability into your core design was.

Debugging the Development Process: Practical Strategies for Staying Focused, Hitting Ship Dates, and Building Solid Teams.

In our development team, we looked very closely at what we consider to be a programmer error, developer error, design error and how we could use both unit testing and also building reliability into our software package through DBC and following the advice of debugging the development proccess.

查看更多
贪生不怕死
7楼-- · 2019-01-30 18:52

Using tut http://tut-framework.sourceforge.net/ very simple, just header file only no macros. Can generate XML results

查看更多
登录 后发表回答