Use cppunitlite on Windows Mobile to output test results

Source: Internet
Author: User
Background

TDD test-driven development is a popular development method and model. Follow the TDD Method for developmentProgramLibraries are particularly useful because libraries provide certain functional interfaces for third parties. TDD methods can be used to provide test cases for predefined interfaces in advance to ensure implementation.CodeThe test ensures that the Library can implement predefined functions faithfully. The Mobile sensors api library I developed previously has received some bad feedback because I didn't write a unit test. Now I introduce the unit test to the library to improve the quality of the library. For more information about the mobile sensors api library, see the development of the gravity sensor (gravitational sensor) in Windows Mobile. For testing native C ++ in Windows Mobile and Windows Mobile, refer to unit testing in Windows Mobile and Windows Mobile. For testing on the CF platform, refer to unit testing under. NET Compact framework. For an example of TDD, you can refer to the TDD implementation of a digital game on Windows Mobile.

Introduction

This article describes how to output the test results to the file development when cppunitlite is used in Windows Mobile to perform unit test on native C ++. It supports the Mobile sensors api library.

Implementation

Because the original cppunitlite only supports printing the test results to the standard output, but Windows Mobile does not have the console output by default, You need to modify cppunitlite to support inputting the results into the file.

Modify Definition
 #include   
    
    
class failure;
class testresult
{< br> Public :
testresult ( char * _ filename = 0 );
virtual void testsstarted ();
virtual void addfailure ( const failure & failure );
virtual void testsended ();
private :< br> int failurecount;
char * filename;
file * filestream;
};

To implement this function, you need to modify the testresult class. First, add the filename variable to keep the output file path and name, and add the filestream to save the file stream handle. Then modify the testresult constructor. The original constructor does not have any input parameters, such as testresult (). modify it to add the file path and name input parameters. Testresult (char * _ filename = 0); To maintain source code compatibility with the original client code, this parameter has a default value of 0 (null, null ). The client source code-level compatibility here means that the client code can support the new database after modification without any modifications. This complies with the open-close principle in the software design process. However, this modification does not support runtime compatibility. The client needs to re-compile (because the header file is modified) and link (because the cppunitlite static library is updated ).

Update implementation
 
Testresult: testresult (Char* _ Filename)
: Failurecount (0 ),
Filename (_ filename)
{
}

Modify the constructor and save the input to the member variable.

 
VoidTestresult: testsstarted ()
{
If(Filename! = NULL)
{
Filestream = fopen (filename,"W");
}
Else
{
Filestream = stdout;
}
}

Modify the startup test function. When the file name is not empty, generate the output file stream ). If it is null, use the standard output stream.

 
VoidTestresult: addfailure (ConstFailure & failure)
{
Fprintf (filestream,"% S % LD % S % s \ n",
"Failure :\"",
Failure. Message. ascharstring (),
"\"",
"Line",
Failure. linenumber,
"In",
Failure. filename. ascharstring ());

Failurecount ++;
}

Output Error information to the output stream.

VoidTestresult: testsended ()
{
If(Failurecount> 0)
Fprintf (filestream,"There were % LD failures \ n", Failurecount );
Else
Fprintf (filestream,"There were no test failures \ n");

If(Filename! = NULL & filestream! = NULL)
{
Fflush (filestream );
Fclose (filestream );
Filestream = NULL;
}
}

Clear resources. In Windows Mobile and wince, you sometimes need to explicitly call fflush. Otherwise, some outputs cannot be synchronized to files.

Client Modification
 
// Unit test
Testresult tr ("Testresult.txt");
Testregistry: runalltests (TR );

It is easy to modify the client code. You only need to pass the file name in the object that generates testresult. If no absolute path is passed, the file will be generated to the root directory.

Mobile sensors API Project

This project is still in its infancy. Currently, Samsung's gravity sensor is implemented. I host the project to Mobile sensors API-native uniied APIs for Windows Mobile sensors, and I will continue to improve it, implement various sensors in this project.

Because I don't have an HTC machine on hand, if anyone is interested, I can join the project to help me test the HTC device. Because I joined the unit test, the test became very simple and only needed to execute the program, you can refer to the test output file without debugging. Of course, this test process is a continuous iteration process, but unit test simplifies the sub-process.

Source code: Http://mobilesensor.codeplex.com/SourceControl/ListDownloadableCommits.aspx

Environment: vs2008 + WM 6 Professional SDK + Samsung Windows Mobile SDK

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.