If you have not yet set up the Gtest framework, you can refer to my previous blog: http://www.cnblogs.com/jycboy/p/6001153.html.
1.The First Sample:sample1
After you have brought the project on github, GitHub address: https://github.com/google/googletest, in the Directory:. (your Directory) \googletest-master\googletest\samples is your samples folder.
Create a project in Vs: Gtestsamples
Add the corresponding code to the side: sample1.h, sample1.cc, sample1_unittest.cc.
In sample1.cc is the function you want to test:
Returns n! (the factorial of n). For negative n, n! is defined to be 1.//int factorial (int n) {int result = 1;for (int i = 1; i <= n; i++) {result *= i;} Return result;} Returns true iff N is a prime number.bool isprime (int n) {//Trivial case 1:small numbersif (n <= 1) return false;/ /Trivial case 2:even numbersif (n 2 = = 0) return n = = 2;//now, we had that n was odd and n >= 3.//Try to divide n By every odd number i, starting from 3for (int i = 3;; i + = 2) {//We only has to try I up to the squre root of NIF (i > N/i) break;//now, We had I <= n/i < n.//If n is divisible by i, N was not prime.if (n% i = = 0) return fals e;} n have no integer factor in the range (1, n), and thus is Prime.return true;}
In sample1_unittest.cc, you write the test:
There are two test cases: factorial, isprime; Each test case corresponds to three tests.
extern int factorial (int n), extern bool IsPrime (int n),//Tests factorial ().//Tests factorial of negative numbers. Test (factorialtest, negative) {//this test is named ' Negative ', and belongs to the ' Factorialtest '//test case. Expect_eq (1, factorial (-5)); Expect_eq (2, factorial (-1)); If it is assert_eq, the rear expect_gt will not be executed, and if it is expect_eq, the back test expect_gt continue to perform expect_gt (factorial (-10), 0); Assert_eq (3, factorial ( -1));//<technicaldetails>////expect_eq (expected, actual) is the same as////expect_true ( (expected) = = (actual))////except that it would print both the expected value and the actual//value when the assertion FA Ils. This is very helpful for//debugging. Therefore in this case expect_eq are preferred.////on the other hand, expect_true accepts any Boolean Expression,//and is Thus more general.////</technicaldetails>}//Tests factorial of 0.TEST (factorialtest, Zero) {expect_eq (1, Factori Al (0));} Tests Factorial of positive numbers. TEST (factorialtest, Positive) {expect_eq (1, FActorial (1)); Expect_eq (2, factorial (2)); Expect_eq (6, factorial (3)); Expect_eq (40320, factorial (8));} Tests isprime ()//Tests Negative Input. Test (isprimetest, negative) {//this test belongs to the Isprimetest test Case. Expect_false (isprime (-1)); Expect_false (isprime (-2)); Expect_false (isprime (int_min));} Tests some trivial cases. TEST (isprimetest, Trivial) {expect_false (isprime (0)); Expect_false (isprime (1)); Expect_true (isprime (2)); Expect_true (isprime (3));} Tests Positive Input. TEST (isprimetest, Positive) {expect_false (isprime (4)); Expect_true (isprime (5)); Expect_false (isprime (6)); Expect_true (isprime (23));} Step 3. Call Run_all_tests () in main ().////We does this by linking in src/gtest_main.cc file, which consists of//a main () function Which calls Run_all_tests () for us.////this runs all the TESTS you ' ve defined, prints the result, and//returns 0 if suc cessful, or 1 Otherwise.////did you notice that we didn ' t register the tests? The//run_all_tests () macro magically knows about all The Tests We//defined. Isn ' t this convenient?
2. Steps for writing unit tests
Step 1. Include the necessary header files in order to declare what the test logic NEEDS. Don't forget GTEST.H.
#include <limits.h> #include "sample1.h" #include <gtest/gtest.h>
Step 2. Use the test macro to define the Tests.
The test has two parameters: the testing case name and the test Name.
After you use a macro, you should define test logic between a pair of curly Braces. You can use a bunch of macros to indicate the success or failure of a test. Expect_true and Expect_eq are examples of such macros. For a complete list, see GTEST.H.
Technical Details:
In Google test, the test is divided into multiple test cases. You should put logically related tests into the same test case.
Test case names and test names should be valid C + + Identifiers. And you should not use underscores (_) in the Name.
Google testing guarantees that each test you define runs only once, but does not guarantee the order in which the tests are Executed. therefore, you should write tests in such a way that their results do not depend on the order in which they are made.
Test (factorialtest, negative) { //this test is named ' Negative ', and belongs to the ' factorialtest ' //test case. expect_eq (1, factorial ( -5)); Expect_eq (2, factorial (-1)); If it is assert_eq, the rear expect_gt will not be executed, and if it is expect_eq, the back test expect_gt continue to perform expect_gt (factorial ( -10), 0); Expect_true (2 = = Factorial (-7));//this error failure message Prints TRUE or false, the value is not printed
Technical Details:
The above expect_eq (1, factorial (-1)) and expect_true (1==factorial (-1)) are the same, but when they fail, expect_eq prints the expected and actual values, and expect_ True to print true, False. So we prefer expect_eq.
Step 3. Call Run_all_tests () in main ().
int main (int argc, char **argv) {//printf ("Running main () from gtest_main.cc\n"), testing::initgoogletest (&argc, argv); return run_all_tests ();}
all tests are automatically called by **run_all_tests ().
Previous two blog posts:
VS2015 Building Googletest framework--configuring The first project introduction to the Google C + + unit testing Framework---gtest Framework
Then will be updated gradually, until gmock, I hope I will not be too lazy, ....
Steps of the Google C + + unit testing framework---gtest Sample1 and writing unit tests