简体   繁体   English

Python3中使用单元测试和正常做测试的区别

[英]Difference between using unit test and doing tests normally in Python3

What is actually the difference between using unit tests and normal tests?使用单元测试和普通测试之间实际上有什么区别?

By normal tests I mean, using the if statement for example to determine if the calculation is equal to the desired answer if it returns false, we can raise AssertionError通过normal tests ,我的意思是,例如使用 if 语句来确定计算是否等于所需的答案,如果它返回 false,我们可以引发AssertionError

Let's use a simple piece of code as example:让我们以一段简单的代码为例:

def square(a):
   return a*a;

Tests that aim at finding bugs in this function in isolation are unit-tests.旨在单独查找此 function 中的错误的测试是单元测试。 This is independent of how you actually implement them: If you just write an if statement like if (square(3) != 9) and raise an AssertionError as you say, then it is a unit-test.这与您实际实现它们的方式无关:如果您只是编写if (square(3) != 9)之类的if语句并按照您所说的那样引发AssertionError ,那么它就是一个单元测试。 If instead you implement the same test using unittest by calling assertEqual , then it is also a unit-test.相反,如果您通过调用assertEqual使用unittest实现相同的测试,那么它也是一个单元测试。

In other words, whether or not you use a (so-called) unit-test framework is not a criterion for whether your tests are unit-test or not.换句话说,您是否使用(所谓的)单元测试框架并不是您的测试是否为单元测试的标准。 In fact, despite the names of the frameworks ('unittest' in Python, 'JUnit' in Java, ...) these frameworks can be used for unit-tests as well as for other tests, like, integration-tests.事实上,尽管有框架的名称(Python 中的“unittest”,Java 中的“JUnit”,......)这些框架可以用于单元测试以及其他测试,例如集成测试。 The names of those frameworks are therefore a bit misleading.因此,这些框架的名称有点误导。

So much for the original question about the difference between unit-tests and normal tests.关于单元测试和普通测试之间区别的原始问题就这么多。 In one of the comments you make it clearer that you actually want to know what is better: To use or not to use a test framework.在其中一条评论中,您更清楚地表明您实际上想知道什么更好:使用或不使用测试框架。 And, the answer is clear: Definitely go for a test framework.而且,答案很明确:绝对是 go 用于测试框架。

After writing only a few tests 'by hand', that is, without a test framework, you will see that there is a lot of duplication in your test code: You compare results with if - that is not so much different.在“手工”编写了几个测试之后,也就是说,没有测试框架,你会发现你的测试代码中有很多重复:你将结果与if进行比较 - 这并没有太大的不同。 But then, in the success case you write some 'passed' message, with the test name, in the failure case you write a 'failed' messages, again with test name and in this case also with some extra information about what was the actual and what was the expected result.但是,在成功的情况下,你写一些“通过”的消息,带有测试名称,在失败的情况下,你写一个“失败”的消息,同样带有测试名称,在这种情况下,还有一些关于实际情况的额外信息什么是预期的结果。 And - did you think about the case that the code under test exits with an exception?而且——你有没有想过被测代码异常退出的情况? So, you also have to wrap it with a try/catch block to make sure unexpected exceptions lead to a 'failed' result, again with some helpful diagnostic information.因此,您还必须使用 try/catch 块对其进行包装,以确保意外异常导致“失败”结果,并再次提供一些有用的诊断信息。

And so on... All this and more is taken care of by the test frameworks for you.等等......所有这些以及更多内容都由测试框架为您处理。

From your question, you assume there are two kinds of test types: unit tests and normal test .根据您的问题,您假设有两种测试类型:单元测试普通测试 In fact, there is a lot of types of tests.事实上,有很多类型的测试。 The unit test is one of them.单元测试就是其中之一。 You can read here other kinds of tests.您可以在此处阅读其他类型的测试。

If we assume 'normal test' is 'black box test'.如果我们假设“正常测试”是“黑盒测试”。 It is testing software without source code.它正在测试没有源代码的软件。 For unit tests, there is another part of the software that tests the integrity of the source code itself.对于单元测试,软件的另一部分测试源代码本身的完整性。 What does that mean?这意味着什么? You wrote a function that sums two numbers.你写了一个 function 对两个数字求和。 For the unit test, you are writing another function to check whether your first function works对于单元测试,您正在编写另一个 function 以检查您的第一个 function 是否有效

# Your Function
def my_sum_function(x,y):
    return(x+y)

# Your Unit Test Function
def test_sum():
    assert my_sum_function([1, 2]) == 3, "Should be 6"

Not only for Python but also for all programming language logic behind the test is the same.不仅对于Python而且对于所有编程语言背后的逻辑测试都是一样的。

For quality control.用于质量控制。 If you are coding an app called "app1" with a function f1 and an other function f2 (f2 is using f1) inside you own library如果您在自己的库中使用 function f1 和另一个 function f2(f2 使用 f1)编写名为“app1”的应用程序

Without unit test: You can make a change in function f1 test it, it will work in f2 but you will not notice that in you program "app0" you wrote 2 weeks ago it will make you app crash because in your old app "app0" you have a function f3 and f3 is also using f1....没有单元测试:您可以在 function 中进行更改 f1 测试它,它将在 f2 中工作,但您不会注意到在您两周前编写的程序“app0”中它会使您的应用程序崩溃,因为在您的旧应用程序“app0 " 你有一个 function f3 并且 f3 也在使用 f1....

With unit test: You can catch that type of nasty bug because the test for the function f3 will not pass anymore and you IDE will tell you that when you change the function f1 in "app1"使用单元测试:您可以捕获那种讨厌的错误,因为 function f3 的测试将不再通过,您 IDE 会告诉您,当您在“7”应用程序中更改 ZC1C425268E68385D1AB5014F14C1 时

I hope it make sense (english is not my native language)我希望它有意义(英语不是我的母语)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM