一个简单的单元测试框架 [英] A simple unit test framework

查看:67
本文介绍了一个简单的单元测试框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我之前曾询问有关用C ++教授测试的建议。基于我收到的一些回复的
我决定最好的方式继续

将教会学生如何编写他们自己的单位

测试框架,然后在实验室会话中看看我是否可以让他们自己编写
。为了给他们一个例子,我创建了以下

UTF类(下面是一个简单的测试程序)。我会欢迎并提出
关于这里有人如何感觉可以改进的建议:


感谢您的时间!


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_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<< ******测试: << current_description<<

std :: endl;

}


void end_test_set(){

std :: cout<< ******测试: << current_description<<

完成,;

std :: cout<< 通过 << tests_passed<< 失败 <<

tests_failed<< "。" << std :: endl;

}


模板< 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<< "不等于 << t2<<

std :: endl;

total_tests_failed ++;

tests_failed ++;

} else {tests_passed ++ ; total_tests_passed ++; }

}


void test_report(){

std :: cout<< ***测试集: << test_set_name<< "完成,;

std :: cout<< 通过 << total_tests_passed;

std :: cout<< "失败了 << total_tests_failed<< "。" << std :: endl;

if(total_tests_failed!= 0)std :: cout<< ***测试失败! <<

std :: endl;

}

};


int main (void){

//在位置0,0处创建一个矩形,边长为10

UnitTest ut(Test Shapes);


//测试类矩形

ut.begin_test_set(" Rectangle",__ FILE__);

矩形r(0,0,10,10) );

ut.test(r.is_square(),true,__ LINE__);

ut.test(r.area(),100.0,__ LINE __);


矩形r2(0,0,1,5);

ut.test(r2.is_square(),true,__ LINE__);

ut.test(r2.area(),5.0,__ LINE__);

ut.end_test_set();


//测试类圈

ut.begin_test_set(" Circle",__ FILE__);

圈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();


返回0;

}

决方案
NW写道:

我以前问关于使用C教学测试++建议。基于我收到的一些回复的
我决定最好的方式继续

将教会学生如何编写他们自己的单位

测试框架,然后在实验室会话中看看我是否可以让他们自己编写
。为了给他们一个例子,我创建了以下

UTF类(下面是一个简单的测试程序)。我会欢迎并提供关于这里有人如何改进的建议:

http://cxxtest.sourceforge.net/

这是我一直使用的c ++单元测试框架的链接。拿一个

的样子 - 你可能会想到如何改进你的单元测试框架...


nw写道:
< blockquote class =post_quotes>
>

我之前曾询问有关用C ++教授测试的建议。基于我收到的一些回复的
我决定最好的方式继续

将教会学生如何编写他们自己的单位

测试框架,然后在实验室会话中看看我是否可以让他们自己编写
。为了给他们一个例子,我创建了以下

UTF类(下面是一个简单的测试程序)。我会欢迎并提出
关于这里有人如何感觉可以改进的建议:



使用工具的傻瓜仍然是个傻瓜。测试中的挑战不是测试管理,而是设计测试用例以涵盖测试代码中可能出现的故障。这是大多数开发人员不做的事情,因为他们的重点是让代码运行。一个好的测试员

专注于让代码失败。


-


- Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:教程和
参考的作者。 ( www.petebecker.com/tr1book


一个带工具的傻瓜仍然是个傻瓜。测试中的挑战不是测试


管理,而是设计测试用例以涵盖测试中代码可能出现的故障。这是大多数开发人员不做的事情,因为他们的重点是让代码运行。一个好的测试人员

专注于让代码失败。



同意。这是我提供一个相对简单的小型
类的动机,它实际上只是一个失败的比较函数

打印出文件并测试失败的行。所以我我要花大约半小时的时间来讨论他们需要的C ++的功能

__LINE __,__ FILE__等,并引入一个简单的框架。然后

另外半小时谈论设计测试以尝试使他们的

代码失败。


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;
}

解决方案

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...


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)


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.


这篇关于一个简单的单元测试框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆