Monday, May 10, 2010

use googletest on windows ce

I've been trying to use googletest on windows ce platform to do unit testing. But gtest doesn't provide a windows ce project file, so I had to modify the project myself. Here is how to do so:

1. Add a new platform
I added a new windows ce based platform (Windows Mobile 5.0 Pocket PC SDK (ARMV4I), for example) in the configuration manager of the gtest project.


2. Add below preprocessor definitions
 In order to compile gtest library for windows ce, I used below preprocessor definitions:
 "NDEBUG;_WIN32_WCE=$(CEVER);UNDER_CE;$(PLATFORMDEFINES);WINCE;_CONSOLE;$(ARCHFAM);$(_ARCHFAM_);_UNICODE;UNICODE"

Not all of them are mandatory, but missing the bold ones may cause gtest fail to compile.


3. Create a windows ce console project
The sample project is available at:
http://code.google.com/p/rxwen-blog-stuff/source/browse/#svn/trunk/wince/ce_gtest_proj
This project expects to find gtest header file (gtest\gtest.h) and static library (gtestd.lib) in googletest folder in parent directory.

4. Run the application on emulator/device and verify output
Finally, I run the unit testing application on a windows ce device, and get below outputs in visual studio and serial port output respectively. The output shows that the test case passed successfully.
visual studio output window

device serial port output



It's not always necessary to run unit testing application on windows ce device. If we write our application with care, it's possible that the application can compile and run on both win32 and windows ce platform. Then we can do unit testing on a normal pc, which will be easier and faster.
But I still would like to run the unit testing on windows ce if our product is supposed to run on it. Any subtle differences between win32 and win ce may cause the unit testing succeed on one platform but fail on the other. It's wise to do unit testing on the platform that the application will actually run.

9 comments:

Philipp Kursawe said...

Nice one! Just the thing I needed now. However I cannot get the gtest.lib to link with my CE Console Project.

gtestd.lib(gtest.obj) : error LNK2019: unresolved external symbol "public: void __cdecl testing::TestPartResultArray::Append(class testing::TestPartResult const &)" (?Append@TestPartResultArray@testing@@QAAXABVTestPartResult@2@@Z) referenced in function "public: virtual void __cdecl testing::ScopedFakeTestPartResultReporter::ReportTestPartResult(class testing::TestPartResult const &)" (?ReportTestPartResult@ScopedFakeTestPartResultReporter@testing@@UAAXABVTestPartResult@2@@Z)
gtestd.lib(gtest.obj) : error LNK2019: unresolved external symbol "public: class testing::TestPartResult const & __cdecl testing::TestPartResultArray::GetTestPartResult(int)const " (?GetTestPartResult@TestPartResultArray@testing@@QBAABVTestPartResult@2@H@Z) referenced in function "class testing::AssertionResult __cdecl testing::internal::HasOneFailure(char const *,char const *,char const *,class testing::TestPartResultArray const &,enum testing::TestPartResult::Type,char const *)" (?HasOneFailure@internal@testing@@YA?AVAssertionResult@2@PBD00ABVTestPartResultArray@2@W4Type@TestPartResult@2@0@Z)
gtestd.lib(gtest.obj) : error LNK2019: unresolved external symbol "public: int __cdecl testing::TestPartResultArray::size(void)const " (?size@TestPartResultArray@testing@@QBAHXZ) referenced in function "class testing::AssertionResult __cdecl testing::internal::HasOneFailure(char const *,char const *,char const *,class testing::TestPartResultArray const &,enum testing::TestPartResult::Type,char const *)" (?HasOneFailure@internal@testing@@YA?AVAssertionResult@2@PBD00ABVTestPartResultArray@2@W4Type@TestPartResult@2@0@Z)
gtestd.lib(gtest.obj) : error LNK2019: unresolved external symbol "private: static class testing::internal::String __cdecl testing::TestPartResult::ExtractSummary(char const *)" (?ExtractSummary@TestPartResult@testing@@CA?AVString@internal@2@PBD@Z) referenced in function "public: __cdecl testing::TestPartResult::TestPartResult(enum testing::TestPartResult::Type,char const *,int,char const *)" (??0TestPartResult@testing@@QAA@W4Type@01@PBDH1@Z)
gtestd.lib(gtest.obj) : error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl testing::operator<<(class std::basic_ostream > &,class testing::TestPartResult const &)" (??6testing@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABVTestPartResult@0@@Z) referenced in function "void __cdecl GTestStreamToHelper(class std::basic_ostream > *,class testing::TestPartResult const &)" (??$GTestStreamToHelper@VTestPartResult@testing@@@@YAXPAV?$basic_ostream@DU?$char_traits@D@std@@@std@@ABVTestPartResult@testing@@@Z)
Windows Mobile 6 Professional SDK (ARMV4I)\Debug/cbvtests.exe : fatal error LNK1120: 5 unresolved externals


Any ideas?

Unknown said...

The gtest lib fail to link with something defined in the same project.
I guess there is something wrong with your gtest project file. Can you post the project file somewhere so that I can take a look at it?

Philipp Kursawe said...

Thanks for your quick response!
It was an error that the project convert assist caused. It did not include the gtest-test-part.cc into the project. After adding it manually the created lib was correct and could be linked.
Thanks for your article!

One more question though: how do I get the coloured output in the VStudio output window? I tried adding the color flag to the command line to no avail.

Unknown said...

You can configure the output window's color within visual studio from:
Tools - Options - Environment - Fonts and Colors - Select Output Window in Show settings for - Pick Item foreground and Item background color.

Philipp Kursawe said...

I know :) I meant the coloured output of googletest. It can also print failed tests in red to the console.

Unknown said...

Embarrassing :)

I don't think visual studio's output window supports colorful output, so I guess it's not feasible.

xtian said...

Hello. I'm trying to use googletest in pocket pc as well. Did you configure anything else to have the results/output be displayed on visual studio's debug output window?

Thanks in advance.

Unknown said...

No, I didn't make extra configurations.
It worked if I started debugging application within visual studio.
What result did you get?

xtian said...

It works now. I was previously using the Pocket PC 2003 emulator and got no output either on VS debug output or serial port output.

I tried to used the WM 5 emulator and I got something on serial port output.

Then, while I was trying to have my unit test application run on the emulator, I needed to also deploy some DLLs. And then I noticed that I am also getting the output on VS debug window.

Thanks!