Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A simple unit test framework

Reply
Thread Tools

A simple unit test framework

 
 
nw
Guest
Posts: n/a
 
      05-03-2007
Hi,

I previously asked for suggestions on teaching testing in C++. Based
on some of the replies I received I decided that best way to proceed
would be to teach the students how they might write their own unit
test framework, and then in a lab session see if I can get them to
write their own. To give them an example I've created the following
UTF class (with a simple test program following). I would welcome and
suggestions on how anybody here feels this could be improved:

Thanks for your time!

class UnitTest {
private:
int tests_failed;
int tests_passed;
int total_tests_failed;
int total_tests_passed;
std::string test_set_name;
std::string current_file;
std::string current_description;

public:

UnitTest(std::string test_set_name_in) : tests_failed(0),

tests_passed(0),

total_tests_failed(0),

total_tests_passed(0),

current_file(),

current_description(),

test_set_name(test_set_name_in) {
std::cout << "*** Test set : " << test_set_name << std::endl;
}

void begin_test_set(std::string description, const char *filename) {
current_description = description;
current_file = filename;
tests_failed = 0;
tests_passed = 0;
std::cout << "****** Testing: " << current_description <<
std::endl;
}

void end_test_set() {
std::cout << "****** Test : " << current_description << "
complete, ";
std::cout << "passed " << tests_passed << ", failed " <<
tests_failed << "." << std::endl;
}

template<class _TestType>
bool test(_TestType t1,_TestType t2,int linenumber) {
bool test_result = (t1 == t2);

if(!test_result) {
std::cout << "****** FAILED : " << current_file << "," <<
linenumber;
std::cout << ": " << t1 << " is not equal to " << t2 <<
std::endl;
total_tests_failed++;
tests_failed++;
} else { tests_passed++; total_tests_passed++; }
}

void test_report() {
std::cout << "*** Test set : " << test_set_name << " complete, ";
std::cout << "passed " << total_tests_passed;
std::cout << " failed " << total_tests_failed << "." << std::endl;
if(total_tests_failed != 0) std::cout << "*** TEST FAILED!" <<
std::endl;
}
};

int main(void) {
// create a rectangle at position 0,0 with sides of length 10
UnitTest ut("Test Shapes");

// Test Class Rectangle
ut.begin_test_set("Rectangle",__FILE__);
Rectangle r(0,0,10,10);
ut.test(r.is_square(),true,__LINE__);
ut.test(r.area(),100.0,__LINE__);

Rectangle r2(0,0,1,5);
ut.test(r2.is_square(),true,__LINE__);
ut.test(r2.area(),5.0,__LINE__);
ut.end_test_set();

// Test Class Circle
ut.begin_test_set("Circle",__FILE__);
Circle c(0,0,10);
ut.test(c.area(),314.1592654,__LINE__);
ut.test(c.circumference(),62.831853080,__LINE__);

ut.end_test_set();

ut.test_report();

return 0;
}

 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      05-03-2007
nw wrote:
> I previously asked for suggestions on teaching testing in C++. Based
> on some of the replies I received I decided that best way to proceed
> would be to teach the students how they might write their own unit
> test framework, and then in a lab session see if I can get them to
> write their own. To give them an example I've created the following
> UTF class (with a simple test program following). I would welcome and
> suggestions on how anybody here feels this could be improved:


http://cxxtest.sourceforge.net/
Here is a link to the c++ unit test framework I have been using. Take a
look - you might get an idea how to improve your unit test framework...
 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      05-03-2007
nw wrote:
>
> I previously asked for suggestions on teaching testing in C++. Based
> on some of the replies I received I decided that best way to proceed
> would be to teach the students how they might write their own unit
> test framework, and then in a lab session see if I can get them to
> write their own. To give them an example I've created the following
> UTF class (with a simple test program following). I would welcome and
> suggestions on how anybody here feels this could be improved:
>


A fool with a tool is still a fool. The challenge in testing is not test
management, but designing test cases to cover the possible failures in
the code under test. That's something that most developers don't do
well, because their focus is on getting the code to run. A good tester
focuses on getting the code to fail.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
nw
Guest
Posts: n/a
 
      05-03-2007
> A fool with a tool is still a fool. The challenge in testing is not test
> management, but designing test cases to cover the possible failures in
> the code under test. That's something that most developers don't do
> well, because their focus is on getting the code to run. A good tester
> focuses on getting the code to fail.


Agreed. That was my motivation in providing a relatively simple small
class which is really just a comparison function that on failure
prints out the file and line the test failed in. So I was going to
spend about a half hour talking about the features of C++ they'll need
__LINE__, __FILE__ etc. and introducing a simple framework. Then
another half hour talking about designing tests to try and make their
code fail.

 
Reply With Quote
 
nw
Guest
Posts: n/a
 
      05-03-2007
> A fool with a tool is still a fool. The challenge in testing is not test
> management, but designing test cases to cover the possible failures in
> the code under test. That's something that most developers don't do
> well, because their focus is on getting the code to run. A good tester
> focuses on getting the code to fail.


Agreed. That was my motivation in providing a relatively simple small
class which is really just a comparison function that on failure
prints out the file and line the test failed in. So I was going to
spend about a half hour talking about the features of C++ they'll need
__LINE__, __FILE__ etc. and introducing a simple framework. Then
another half hour talking about designing tests to try and make their
code fail.

 
Reply With Quote
 
nw
Guest
Posts: n/a
 
      05-03-2007
> A fool with a tool is still a fool. The challenge in testing is not test
> management, but designing test cases to cover the possible failures in
> the code under test. That's something that most developers don't do
> well, because their focus is on getting the code to run. A good tester
> focuses on getting the code to fail.


Agreed. That was my motivation in providing a relatively simple small
class which is really just a comparison function that on failure
prints out the file and line the test failed in. So I was going to
spend about a half hour talking about the features of C++ they'll need
__LINE__, __FILE__ etc. and introducing a simple framework. Then
another half hour talking about designing tests to try and make their
code fail.

 
Reply With Quote
 
nw
Guest
Posts: n/a
 
      05-03-2007
> A fool with a tool is still a fool. The challenge in testing is not test
> management, but designing test cases to cover the possible failures in
> the code under test. That's something that most developers don't do
> well, because their focus is on getting the code to run. A good tester
> focuses on getting the code to fail.


Agreed. That was my motivation in providing a relatively simple small
class which is really just a comparison function that on failure
prints out the file and line the test failed in. So I was going to
spend about a half hour talking about the features of C++ they'll need
__LINE__, __FILE__ etc. and introducing a simple framework. Then
another half hour talking about designing tests to try and make their
code fail.

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      05-03-2007
nw wrote:
>> A fool with a tool is still a fool. The challenge in testing is not test
>> management, but designing test cases to cover the possible failures in
>> the code under test. That's something that most developers don't do
>> well, because their focus is on getting the code to run. A good tester
>> focuses on getting the code to fail.

>
> Agreed. That was my motivation in providing a relatively simple small
> class which is really just a comparison function that on failure
> prints out the file and line the test failed in. So I was going to
> spend about a half hour talking about the features of C++ they'll need
> __LINE__, __FILE__ etc. and introducing a simple framework. Then
> another half hour talking about designing tests to try and make their
> code fail.
>


Saying it four times doesn't make your point any stronger

I would only suggest that you also try to add a test registry and some
macros so that __FILE__ and __LINE__ are not used in test cases.

In the Austria C++ unit test system, I use exceptions to indicate
failure. It's usually silly to continue with a test if part of it has
failed.

Is Austria C++ there is also an assert macro "AT_TCAssert" for "Test
Case Assert" which is somewhat similar to:

ut.test(r.is_square(),true,__LINE__);

AT_TCAssert throws a "at::TestCase_Exception" when the assert fails and
provides a string descrbing the error.

Here is an example:

AT_TCAssert( m_value == A_enum, "Failed to get correct type" )

#define AT_TCAssert( x_expr, x_description ) \
if ( !( x_expr ) ) { \
throw TestCase_Exception( \
AT_String( x_description ), \
__FILE__, \
__LINE__ \
); \
} \
// end of macro

.... now that I think about it, that should be a while() not an if() or
an if wrapped in a do {}.

TestCase_Exception also grabs a stack trace and can print out a trace of
the place it is thrown.



 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-04-2007
Pete Becker wrote:
> nw wrote:
>>
>> I previously asked for suggestions on teaching testing in C++. Based
>> on some of the replies I received I decided that best way to proceed
>> would be to teach the students how they might write their own unit
>> test framework, and then in a lab session see if I can get them to
>> write their own. To give them an example I've created the following
>> UTF class (with a simple test program following). I would welcome and
>> suggestions on how anybody here feels this could be improved:
>>

>
> A fool with a tool is still a fool. The challenge in testing is not test
> management, but designing test cases to cover the possible failures in
> the code under test. That's something that most developers don't do
> well, because their focus is on getting the code to run.
>

Unless the test are written first!

--
Ian Collins.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      05-04-2007
Ian Collins wrote:
> Pete Becker wrote:
>> nw wrote:
>>> I previously asked for suggestions on teaching testing in C++. Based
>>> on some of the replies I received I decided that best way to proceed
>>> would be to teach the students how they might write their own unit
>>> test framework, and then in a lab session see if I can get them to
>>> write their own. To give them an example I've created the following
>>> UTF class (with a simple test program following). I would welcome and
>>> suggestions on how anybody here feels this could be improved:
>>>

>> A fool with a tool is still a fool. The challenge in testing is not test
>> management, but designing test cases to cover the possible failures in
>> the code under test. That's something that most developers don't do
>> well, because their focus is on getting the code to run.
>>

> Unless the test are written first!
>


You can't do coverage analysis or any other form of white-box testing on
code that hasn't been written. There is a big difference between a
tester's minset and a develper's mindset, and it's very hard for one
person to do both.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
contradicting unit test regarding blocks pass, bug in unit/test? timr Ruby 2 11-20-2010 06:30 AM
Test::Unit - Ruby Unit Testing Framework Questions Bill Mosteller Ruby 0 10-22-2009 02:02 PM
All of us know about unit test. But what's a unit? Bill David Java 2 06-18-2008 12:40 AM
unit--, a unit test framework for C++ VvanN C++ 5 04-28-2006 10:01 AM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments