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_fai led;
int total_tests_pas sed;
std::string test_set_name;
std::string current_file;
std::string current_descrip tion;
public:
UnitTest(std::s tring test_set_name_i n) : tests_failed(0) ,
tests_passed(0) ,
total_tests_fai led(0),
total_tests_pas sed(0),
current_file(),
current_descrip tion(),
test_set_name(t est_set_name_in ) {
std::cout << "*** Test set : " << test_set_name << std::endl;
}
void begin_test_set( std::string description, const char *filename) {
current_descrip tion = description;
current_file = filename;
tests_failed = 0;
tests_passed = 0;
std::cout << "****** Testing: " << current_descrip tion <<
std::endl;
}
void end_test_set() {
std::cout << "****** Test : " << current_descrip tion << "
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_fai led++;
tests_failed++;
} else { tests_passed++; total_tests_pas sed++; }
}
void test_report() {
std::cout << "*** Test set : " << test_set_name << " complete, ";
std::cout << "passed " << total_tests_pas sed;
std::cout << " failed " << total_tests_fai led << "." << 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_s et("Rectangle", __FILE__);
Rectangle r(0,0,10,10);
ut.test(r.is_sq uare(),true,__L INE__);
ut.test(r.area( ),100.0,__LINE_ _);
Rectangle r2(0,0,1,5);
ut.test(r2.is_s quare(),true,__ LINE__);
ut.test(r2.area (),5.0,__LINE__ );
ut.end_test_set ();
// Test Class Circle
ut.begin_test_s et("Circle",__F ILE__);
Circle c(0,0,10);
ut.test(c.area( ),314.1592654,_ _LINE__);
ut.test(c.circu mference(),62.8 31853080,__LINE __);
ut.end_test_set ();
ut.test_report( );
return 0;
}
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_fai led;
int total_tests_pas sed;
std::string test_set_name;
std::string current_file;
std::string current_descrip tion;
public:
UnitTest(std::s tring test_set_name_i n) : tests_failed(0) ,
tests_passed(0) ,
total_tests_fai led(0),
total_tests_pas sed(0),
current_file(),
current_descrip tion(),
test_set_name(t est_set_name_in ) {
std::cout << "*** Test set : " << test_set_name << std::endl;
}
void begin_test_set( std::string description, const char *filename) {
current_descrip tion = description;
current_file = filename;
tests_failed = 0;
tests_passed = 0;
std::cout << "****** Testing: " << current_descrip tion <<
std::endl;
}
void end_test_set() {
std::cout << "****** Test : " << current_descrip tion << "
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_fai led++;
tests_failed++;
} else { tests_passed++; total_tests_pas sed++; }
}
void test_report() {
std::cout << "*** Test set : " << test_set_name << " complete, ";
std::cout << "passed " << total_tests_pas sed;
std::cout << " failed " << total_tests_fai led << "." << 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_s et("Rectangle", __FILE__);
Rectangle r(0,0,10,10);
ut.test(r.is_sq uare(),true,__L INE__);
ut.test(r.area( ),100.0,__LINE_ _);
Rectangle r2(0,0,1,5);
ut.test(r2.is_s quare(),true,__ LINE__);
ut.test(r2.area (),5.0,__LINE__ );
ut.end_test_set ();
// Test Class Circle
ut.begin_test_s et("Circle",__F ILE__);
Circle c(0,0,10);
ut.test(c.area( ),314.1592654,_ _LINE__);
ut.test(c.circu mference(),62.8 31853080,__LINE __);
ut.end_test_set ();
ut.test_report( );
return 0;
}
Comment