单元测试 [英] unittesting

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

问题描述

bool fun( int a, int b)
{
  if(a>100)&&(b<1000)
  return a-b;
  if(a<100 && b>1000)
  return b-a;
  if(a<100 && b<1000)
  return a+b;
}



如何测试此代码?
我应该如何编写测试用例?



How can I test this code?
How should I write test cases?

推荐答案



这段代码甚至都不会编译,因为您缺少括号.首先,您需要可以正确编译和运行的代码.然后考虑如果a == 100会发生什么?对于这种情况没有任何回报,请尝试覆盖a和b的所有值.

要测试此代码,您需要使用许多可能的值对其进行调用.编写一些测试代码,并与您的示例代码一起提问,然后人们可能会提出改进建议.我想我要说的是该功能非常简单,所以自己动手做吧.

Ali:)
Hi,

This code is not even going to compile because you have missing brackets. First you need code that compiles and runs without errors. Then think about what happens if a == 100? There is no return for this situation, try to cover all values of a and b.

To test this code you need to call it with many possible values. Write some test code and ask a question with your example code, then people may be able to offer suggestions to improve it. I suppose what I am saying is the function is pretty simple so just have a go yourself.

Ali :)


除了Alison所说的关于实际上拥有可编译的代码...

重要的是,您要在发生有趣的事情的边界周围进行测试-在这种情况下,条件会发生变化.因此,在您的情况下,请查看a的值大约为100,b的值大约为1000.此外,在某些情况下,a = b可能很有趣.

您如何编写测试取决于您对程序员的信心.如果您只是刚开始使用C语言,请保持测试简单:

In addition to what Alison said about actually having code that will compile...

The important thing is that you test around the boundaries where interesting things are happening - in this case where the conditionals are changing. So in your case look at values of a around 100 and values of b around 1000. In addition there''s a couple of cases where a = b might be interesting.

How you write the tests depends on how confident a programmer you are. If you''re just starting out in C keep the tests simple:

assert( fun( 50, 50 ) );



assert是检查一个语句是否为真的宏.它是在assert.h中定义的-您还需要定义DEBUG才能使用它.

如果您更有信心,则可以尝试表驱动方法.定义一个结构,其值为a,值为b以及将这些值输入fun的期望值:



where assert is a macro that checks whether a statement is true. It''s defined in assert.h - you also need to have DEBUG defined to use it.

If you''re a bit more confident then you could try a table driven approach. Define a structure with a value of a, a value of b and the expected value of feeding those values into fun:

struct fun_test
{
    int a;
    int b;
    bool expected_result;
};


这些结构中的每一个都定义一个测试:


Each of these structures defines one test:

struct fun_test test1 = { 50, 50, false };



然后可以编写一个帮助程序来处理测试:



you can then write a helper to process the test:

void process_fun_test( struct fun_test_definition *to_test )
{
    assert( fun( to_test->a, to_test->b ) == to_test->expected_result );
}



您要做的最后一件事是创建一个数组,其中包含您要执行的所有测试,然后遍历整个测试.但是,这正朝着通用的单元测试框架发展,可能对您的需求而言是过高的.

干杯,



PS:如果您对单元测试的另一种哲学感兴趣,请在线查找测试驱动开发".使用TDD,您可以在编写代码之前先编写测试,然后这些测试将充当代码的规范.

PPS:如果这是一门课程的功课,请确保在描述解决方案时从教科书中提取正确的流行语.

PPPS:不知道此代码是否有效,我还没有在编译器上检查过它.

PPPPS:删除了这行,表明我没有阅读Alison的评论-为了侮辱我,我也错误地拼写了她的名字.是时候躺下了方法.



The last thing you do is create an array with all the tests you want to carry out in it and then iterate through the lot. This is, however, getting more towards a general unit testing framework and is probably overkill for your needs.

Cheers,

Ash

PS:If you''re interested in a different philosophy of unit testing look up Test Driven Development online. With TDD you write tests before you write the code and the tests act as the specification of the code.

PPS: If this is homework for a course make sure you pull the correct buzzwords from your textbook when describing your solution.

PPPS: No idea if this code even works, I haven''t checked it on a compiler.

PPPPS: Line removed that showed I didn''t read Alison''s comment - and to add insult to injury I was spelling her name incorrectly as well. Time for a lie down methinks.


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

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