在代码中是否应包含测试语句?

huangapple go评论70阅读模式
英文:

Should we contains test statements in code?

问题

我知道这个问题与技术无关,涉及到代码风格和标准。

我刚学了一年编程,很多人告诉我:“你应该测试程序的每个单独部分,而不是一下子编写所有代码然后测试。”但他们忘了说当正式发布时是否应该删除测试语句?

我听到了几种不同的声音:

  1. 这会让你的代码看起来臃肿,所以要删除它。
  2. 如果以后想要修改某些内容,就不应该删除它。
  3. 你可以删除它,但如果需要的话必须写一些注释。

一个包含测试的简单代码就像这样:(实际代码可能不会像这么简单,这个例子可能有点不好)

void Function(){
    printf("hello,world");
}

int main(){
//  Function();
    return 0;
}

所以我的问题是,我们应该删除它吗?结果是这样的:

void Function(){
    printf("hello,world");
}
int main(){
    return 0;
}
英文:

I know the question is not related to technology,is about the code style and standard.

I have just studied programming for 1 year,and so many people tell me that "you should test any single part of your program rather than code all then test".But they forgot to say whether we should delete test statements when officially released?

I hear several different voice:

  1. It makes your code looks bloated,so delete it.
  2. You shouldn't delete it if you want to modify something later.
  3. You can delete it,but you must write some comment if needed.

A simple code contains test just like this:(the real code won't as easy as it,and the example may be a bit bad)

void Function(){
    printf("hello,world");
}

int main(){
//  Function();
    return 0;
}

So my question is should we delete it ?The result is like this:

void Function(){
    printf("hello,world");
}
int main(){
    return 0;
}

答案1

得分: 5

这并不是测试代码的意思。

相反,您要为代码的每个部分编写单独的测试函数,这些函数可以独立于主要代码运行。然后使用测试运行程序(pytestunittest 或者 nose 是最常用的)来运行您的测试。

例如,假设您的主要代码有这样一个函数:

def add_numbers(a,b):
    return a+b

而您的主函数如下:

def main():
    x = 4
    y = 5
    z = add_numbers(x,y)
    #print(z)  <--- 不要这样做

您可以创建一个新的函数,通常放在一个新的文件中:

def test_add_numbers():
    a=200
    b=343
    res = add_numbers(a,b)
    assert res == 543

测试运行程序将运行所有测试,并输出是否有任何一个测试失败。

英文:

This is not what they mean by testing code though.

Instead you write separate test function for each part of your code, that can run independently of your main code. Then use a test-runner ( pytest, unittest or nose are the most popular) to run your tests.

Example, say your main code has a function like this:

def add_numbers(a,b):
   return a+b

and your main is like this:

def main():
    x = 4
    y = 5
    z = add_numbers(x,y)
    #print(z)  &lt;-- dont do this

instead you create a new function, often in a new file:

def test_add_numbers():
    a=200
    b=343
    res = add_numbers(a,b)
    assert res == 543

Test runners will then run all your tests and output if any one of them doesn't work.

答案2

得分: -2

当官方发布代码库时,代码基础应该有编写好的单元测试,并且每次进行更改时,这些单元测试都应该通过。

测试不应该像你正在编写的方式那样编写,应该使用 pytest 等工具。

英文:

When officially released the code base should have unit tests written and everytime you make a change, those unit tests should pass.

Tests should not be written like you are writing, they should use pytest etc.

huangapple
  • 本文由 发表于 2020年10月7日 17:09:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64240848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定