Simple framework for code test

zhaozj2021-02-16  68

Simple framework for code test

After you write the code, you certainly test it. There are many ways to test code: You can add some breakpoints in your code, or observe the actual situation and compare the results you expected, or write some test programs. Write testing to code is usually the best way to test code. Because the test written can be performed over and over again. When you have modified the implementation, you can run the test again to check if you introduce any bugs. This method can save you from debugging and lead you code with lower error tendencies. As a general policy, when developing your test, we recommend printing the results on the screen; If you print the results in the screen when you run the test, you will analyze these results when you start. However, as time is extended, you will get more and more not pay attention to these data on the screen. Even if the data pointed out an error, you may not be aware. In addition, if other people try to run test, they have to figure out what these printed data mean, this may take a lot of time. In turn, if you use Asserts, people just want to run your test. If an error occurs, they will be prompted in which line has an error, they can correct it. Here is an example of a test routine: #include

#include

/ / Squire a string leader and rear-end blank,

// Return the string after finishing

Std :: String Trim_SPaces (const st: string& str)

{

/ * TRIM_SPACES implementation * /

}

void testtrimspaces ()

{

ASSERT (Trim_Spaces) == "ABC");

ASSERT (Trim_SPaces ("DEF") == "DEF");

Assert (Trim_Spaces) == "this is a test");

ASSERT (Trim_Spaces (") ==" ");

ASSERT (Trim_Spaces (") ==" ");

}

How to build a test framework and build every step:

Step 1: Clearly separate the test code and the actual code. To do this, a very simple method is to put all the documents that use to do test in a special directory. Every name for each file used to test, should start with Test, then add the module / class name it tested. For example, you can see the name of the TestwordTokenizer.cpp file, it is used to test a word tokenizer class.

The test used code is only compiled only when the test is executed. When generating an actual application, the test code is removed during the pre-processing phase. If our test files follow the following mode, you can guarantee this:

// Test

.cpp

#ifdef Testing

/ * TESTING CODE * /

#ENDIF // Testing

// end of file

So, if we define Testing, we are doing testing; otherwise, we are generating applications from the actual code.

Step 2: All the names of the function used to test the code snippet should begin with TEST or TEST. For each module / class you tested, there must be a primary test function to call other test functions to test the various code segments of the module / class. This way you don't need to expose all functions - you can expose the main test function, like the example below: // TestURLUTILITY - Test "URL Utility Function"

#if Defined (Testing)

#include "urlutility.h"

// The function in this namespace is invisible from the exterior of this source file.

Namespace // Anonymous namespace

{

Void testdivideURL ()

{/ * Test code * /}

Void testisurlvalid ()

{/ * Test code * /}

Void teststpurlvalid ()

{/ * Test code * /}

Void TestParsehttpurl ()

{/ * Test code * /}

}; // Anonymous namespace

// ... this is the only visible function outside this source file

//

// We want to expose this function;

// In any file, you can declare its prototype as follows:

// void testURLITY ();

//

// The then you can call in your code.

void testarchility ()

{

TestDivideurl ();

TestisurlValid ();

Testishtpurlvalid ();

Testparsehttpurl ();

}

#ENDIF / / #if Defined (Testing)

// End

Step 3: Create a test primary file (probably called TestMain.cpp), which contains the main function that starts the test. This "main" function is to do is to call a variety of different test functions. You can cancel the comment symbols you want to perform, or comment out the test you don't want to perform. Its code will look like this:

// TestMain.cpp - Test the entire application

#ifdef Testing

#include

// Test function begins

// ... Note: This part contains should be

// Each main test function (test the whole

// Module or whole class function)

Void Testurlutility ();

Void testproxymanager ();

Void TestHttpRequest ();

Void TestHttpheaderfields ();

// The test function ends

/ *

Various parts for testing applications

* /

int main ()

{

Std :: cout << "Testing Application." << std :: endl;

/ / Add to join you to perform

// TestProxyManager ();

// TestHttpRequest ();

// TestURLUTILITY ();

Testhttpheaderfields ();

/ / Cancel the comment symbol you want to perform

Return 0;

}

#ENDIF

// End

Step 4: Some codes should not be compiled (such as user interface code, true "main" function, etc.). These codes will be enclosed in this way:

#ifndef Testing

/ * Actual code * /

#ENDIF // NDEF (Testing)

Step 5: Create a configuration for the test. Most of the latest integrated development environments (IDE) allow selection from different configurations and at least two default configurations: Debug and Release. Create a new one based on Debug Configuration. Then, open the Testing flag directly. If you use GCC, add a -dtesting flag to the compilation parameter. If you use VC6, in Project Settings | C Tab | General | Preprocessor Add a Testing definition ===========

Repost!

转载请注明原文地址:https://www.9cbs.com/read-18123.html

New Post(0)