Test-driven Development TDD (vi) Start Google Test in Windows

Source: Internet
Author: User
Tags greatest common divisor

First, preface
This article will cover some basic uses of gtest, including downloading, installing, compiling, building our first Test demo project, and writing one of the simplest test cases.
Second, download
If you do not remember the URL, directly in Google search Gtest, the first one is. Currently the latest version of Gtest is 1.3.0, which can be downloaded to the latest version from the following address:
Http://googletest.googlecode.com/files/gtest-1.3.0.zip


Http://googletest.googlecode.com/files/gtest-1.3.0.tar.gz


http://googletest.googlecode.com/files/gtest-1.3.0.tar.bz2


Third, compile
After the download decompression, there is a msvc directory:



Using VS students can directly open the msvc inside the project files, if you are using VS2005 or VS2008, open will prompt you to upgrade, after the completion of the level, we directly compile the "Gtest" project, can be directly compiled.
Here to remind that if you upgrade to VS2008 project, then your test demo is the best VS2008 project, otherwise you will find very depressed, your demo how also made up, I have been tossing for a long time, when I upgraded to VS2008 project, As a result, I use the VS2005 project to build a demo. (There is a misunderstanding here, not that it can only be compiled in VS2008, but also in VS2005.) If you are compiling the VS2005 version, it is best to ensure that both gtest and your test project are using VS2005 engineering. )


After compiling, you can see the compiled gtestd.lib or gtest.lib files in the Debug or release directory in MSVC.
Four, the first demo
Below we start to build our first demo, if previously used VS2008 compiled Gtest, then we in VS2008, create a new Win32 Console application. Then you set the project properties, which are summarized as follows:




1.Runtime Library Settings

The settings here are guaranteed to be consistent with the settings when compiling gtest

1.1 Assume that the gtest is set up like this:
Then your project settings should also look like this:



2. Set the Gtest header file path and set the Gtest.lib path

3.1 Copy header file: Place the Gtest folder in the Include directory below the Gtest directory in your $ (scp_root) \include folder

3.2 Copy library files: Copy the compiled gtestd.lib static library files to your project output directory





In the release version, the Runtime library is set to/MT. Of course, you can also choose dynamic Link (/MD), if you have previously compiled gtest also use the same/MD option.
After the project has been set up, let's write one of the simplest test cases, let's write a test function first:

int Foo (int a, int b) {    if (a = = 0 | | b = = 0)    {        throw "Don t do";    }    int C = a% B;    if (c = = 0)        return b;    Return Foo (b, c);}






Yes, the above function is for greatest common divisor. Let's write a simple test case.

#include <gtest/gtest.h> #include "ace/log_msg.h" int Foo (int a, int b) {if (a = = 0 | | b = = 0) {Throw "Don t do";} int c = a% b;if (c = = 0) return B;return Foo (b, c);} TEST (Footest, handlenonezeroinput) {expect_eq (2, Foo (4, 10)); Expect_eq (6, Foo (30, 18));} int ace_tmain (int argc, ace_tchar* argv[]) {testing::initgoogletest (&ARGC, argv); return run_all_tests ();}



As you can see, how easy it is to write a test case. We used the test macro, which has two parameters, the official interpretation of the two parameters is: [Testcasename,testname], and I define these two parameters are: [Testsuitename,testcasename], In the next article, we'll see why this is defined.
Checking the checkpoint, we used the EXPECT_EQ macro, which is used to compare whether two numbers are equal. Google also wraps a series of expect_* and assert_* macros, while the difference between the expect series and the Assert series is:


1. When the expect_* fails, the case continues to execute.


2. When assert_* fails, it is returned directly in the current function, and the statement following assert_* in the current function will not execute.
In the next article, we'll discuss these assertion macros in detail. To get our case up and running, we need to add the following code to the main function:

The above code assumes that you are using the ACE Library, and if not, your main should look like this:


int _tmain (int argc, _tchar* argv[]) {    testing::initgoogletest (&ARGC, argv);    return run_all_tests ();}


"Testing::initgoogletest (&AMP;ARGC, argv);": The Gtest test case allows a series of command-line arguments to be received, so we pass the command-line arguments to gtest for some initialization. The command line parameters of Gtest are very rich and we will learn more about them later.
"Run_all_tests ()": Run all Test Cases


OK, everything is ready, let's run the case directly (a piece of green, very cool):



V. Summary
This article is really very elementary, the purpose is to let the students have never contacted Gtest to understand the basic use of gtest. There are many more advanced ways to use gtest, which we will discuss later. To summarize the contents of this article:


1. Compiling gtest.lib files with vs


2. Set the properties of the test project (the header file, lib file,/MT parameters (and the same parameters used when compiling gtest))


3. Use the test macro to start a test case and set up checkpoints using the Expect_*,asser_* series.


4. Initialize the environment in the main function, and then use the Run_all_test () macro to run the test case.
Advantages:


1. Our test case itself is an EXE project, after compiling can be run directly, very convenient.


2. Writing test cases is very simple (using some simple macros like test), so let's devote more effort to the design and writing of the case.


3. A powerful and rich assertion macro is provided for the inspection of a variety of different checkpoints.


4. Improved the rich command line parameters for a series of settings for the case run.

Reference:

3.Runtime Library Settings

The settings here are guaranteed to be consistent with the settings when compiling gtest



Http://www.cnblogs.com/coderzh/archive/2009/03/31/1426758.html

3.Runtime Library Settings

The settings here are guaranteed to be consistent with the settings when compiling gtest


Test-driven Development TDD (vi) Start Google Test in Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.