Howto install cppunit for MS Visual Studio.Net 2003
cppunit: VS7 Instructions for INSTALL-WIN32.txt
VS7 Instructions for INSTALL-WIN32.txt
2005-07-24 15:25
For newbies to cppunit-1.11.0 and Visual Studio 2003:
Old Instructions:
Frequently Asked Questions: See doc/FAQ
At the current time, the only supported WIN32 platform is
Microsoft Visual C++. You must have VC++ 6.0 at least.
Quick Steps to compile & run a sample using the GUI TestRunner:
- Open examples/examples.dsw in VC++ (contains all the samples).
VC7 will ask you if you want to convert, anwser ‘yes to all’.
- Make HostApp the Active project
- Compile
- in VC++, Tools/Customize…/Add-ins and macro files/Browse…
- select the file lib/TestRunnerDSPlugIn.dll and press ok to register
the add-ins (double-click on failure = open file in VC++).
- Run the project
New Instructions for Visual Studio.Net 2003:
Frequently Asked Questions: See doc/FAQ
Quick Steps to compile & run a sample using the GUI TestRunner:
- Open cppunit-1.11.0/examples/examples.dsw in VC++
( VC7 will ask you if you want to convert, anwser ‘yes to all’ ).
- Make HostApp the Active project
(Open Solution Explorer, Right-click HostApp Project, and click ‘Set as
Startup Project’ )
- Ignore old comments on registering TestRunnerDSPlugIn.dll
- Compile (Build > Build Solution ) (F7)
- Debug > Start (F5)
-TestRunner window will start. Click Browse and Select the tests you
want to run. Choose tests from drop down window and click Run.
System:
Windows XP SP2
Ed
Aug 23, 4:51 pm show options
You still need more steps to run cppunit on your own personal project
in Visual Studio.Net. You must run the previous example first to create
the appropriate libaries for your system.
Steps:
1. Create a new Unmanaged C++ Project.
New > Project > Visual C++ Project > Win32 > Win32 Console Project
1a. Build > Build Solution
2. Open Solution Manager Tab > Right-click the project name >
Properties > Debug
3. Configuration Properties > C++ > General > Additional Include
Directories > (your cppunit directory)\include;(your cppunit
dir)\include\msvc6
example:
C:\workspace\PrsvmCvs\cppunit\include;C:\workspace\PrsvmCvs\cppunit\include\msvc6
4. Configuration Properties > Linker > General > Additional Library
Directories > c:\(location of cppunit on your machine)\lib
-example: C:\workspace\PrsvmCvs\cppunit\lib
5. Configuration Properties > Linker > Input > Additional Dependencies
> cppunitd.lib testrunnerd.lib
–
note: The CppUnit gui doesn’t work for me, but the tests compile and
run in an external cmd line window. I put a breakpoint at the end of
TestRun.cpp, so that the results aren’t erased immediately after the
tests run. I get the correct filename and line number of failed tests
in the command line window.
Ok, now you can make the TestRun.cpp file that manages your unit tests,
and an actual Unit Test class. This unit test tests does a simple test
of a function in a string tokenizer class.
TestRun.cpp
//cppunit required includes
#include
#include
#include
//add includes to your personal unit test files here
#include “TestStringTokenizerAdapter.cpp”
class TestRun
{
public:
bool TestRun::run()
{
CppUnit::TextUi::TestRunner runner;
CppUnit::TestFactoryRegistry ®istry =
CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest( TestStringTokenizerAdapter::suite() );
runner.addTest( registry.makeTest() );
bool wasSucessful = runner.run( “”, false );
return wasSucessful;
}
};
——————–
#include
#include “../StringTokenizerAdapter.h”
class TestStringTokenizerAdapter : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( TestStringTokenizerAdapter );
CPPUNIT_TEST( testConstructors );
CPPUNIT_TEST( setString );
CPPUNIT_TEST_SUITE_END();
CPPUNIT_TEST_SUITE_REGISTRATION( TestStringTokenizerAdapter );
private:
public:
void setUp()
{
}
void tearDown( )
{
}
//@fn open the default options text file
// initialize internal classes
void TestStringTokenizerAdapter::testConstructors(){
//test what the default constructor does
{
StringTokenizerAdapter a;
}
{
StringTokenizerAdapter b( “abc,def,hij,k”, “,” );
CPPUNIT_ASSERT( “abc” == b.nextToken() );
CPPUNIT_ASSERT( “def” == b.nextToken() );
CPPUNIT_ASSERT( “hij” == b.nextToken() );
CPPUNIT_ASSERT( “k232323″ == b.nextToken() );//should fail
}
}
void TestStringTokenizerAdapter::setString(){
StringTokenizerAdapter a;
//now perform some tokenizer functions to see if it worked.
CPPUNIT_ASSERT( “” == a.nextToken() );
a.setString( “abc,def,hij,k”, “,” );
//now perform some tokenizer functions to see if it worked.
CPPUNIT_ASSERT( “abc” == a.nextToken() );
CPPUNIT_ASSERT( “def” == a.nextToken() );
CPPUNIT_ASSERT( “hij” == a.nextToken() );
CPPUNIT_ASSERT( “k33″ == a.nextToken() );//should fail
}
};
In your main class, add:
TestRun t;
t.run();
Ed
And here’s the cmd line window output:
!!!FAILURES!!!
Test Results:
Run: 2 Failures: 2 Errors: 0
1) test: TestStringTokenizerAdapter::testConstructors (F) line: 64
c:\workspace\prsvmcvs\unit_tests\teststringtokenizeradapter.cpp
assertion failed
- Expression: “k232323″ == b.nextToken()
2) test: TestStringTokenizerAdapter::setString (F) line: 80
c:\workspace\prsvmcv
s\unit_tests\teststringtokenizeradapter.cpp
assertion failed
- Expression: “k33″ == a.nextToken()