自己着手在写个完整的全新项目,之前也听说单元测试可以提高程序的健壮性。可是自己找不到合适的资料,对于如何编写单元测试,是一点都不懂。求推荐一些资料,或是单元测试框架~~
迷茫2017-04-17 11:40:28
Someone will definitely recommend you some classic unit testing frameworks, such as GoogleTest, cppunit and so on.
But I can tell you responsibly that those frameworks are too fiddly. You need to spend a lot of time and energy to learn to use them, and then even a very, very small project will take half a day to configure.
Recommend to you an emerging unit testing framework: Catch
How simple is it? You only need to import a header file:
#include "catch.hpp"
In addition, because it is very simple and lightweight, it is easy to learn. After reading this short tutorial, you will basically master it.
In order to understand this framework more intuitively, I post an example from the tutorial:
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
unsigned int Factorial( unsigned int number ) {
return number <= 1 ? number : Factorial(number-1)*number;
}
TEST_CASE( "Factorials are computed", "[factorial]" ) {
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(2) == 2 );
REQUIRE( Factorial(3) == 6 );
REQUIRE( Factorial(10) == 3628800 );
}
This is the whole process of testing a Fibonacci function. How does it feel?
Because your title indicates C++, I preconceptionally thought that you were looking for information on unit testing practice . There are very few unit testing books for the C++ language. I will recommend one:
Modern C++ Programming with Test-Driven Development
If you are interested in the theoretical knowledge of unit test case design, please ask for information. Then I suggest you to learn about TDD-related knowledge, and then you can read some targeted books (I only recommend one, it is very specific):
You can even get some basic understanding of the software testing field first. I recommend a few books:
怪我咯2017-04-17 11:40:28
Please tell me how to use Catch and place the catch.h header file in the project folder. After running the above example, the result returned is:
Is it the reason for garbled characters