软件测试测试python_Python测试简介
软件测试测试 python
数据库系统概论第九章课后答案
by Goran Aviani
通过Goran Aviani
Python测试简介 (An introduction to testing in Python)
You have just finished writing a piece of code and you are wondering what to do. Will you submit a pull request and have your teammates review the code, or will you manually test the code? You should do all these things but with an additional step: you need to unit test your code to make sure that the code works as intended.
python入门教程非常详细word您刚刚编写了⼀段代码,并且想知道该怎么做。您将提交拉取请求并让您的队友查看代码,还是⼿动测试代码?您应该执⾏所有这些操作,但是要执⾏⼀个附加步骤:您需要对代码进⾏单元测试,以确保代码按预期⼯作。
Unit tests can pass or fail, and that makes them a great technique to check your code. In this tutorial I
will demonstrate how to write unit tests in Python and how easy it is to get them going in your own project.
单元测试可以通过或失败,这使它们成为检查代码的好技术。 在本教程中,我将演⽰如何⽤Python编写单元测试,以及如何在您⾃⼰的项⽬中进⾏单元测试。
⼊门 (Getting started)
The best way you can understand testing is if you do it hands-on. For that purpose, in a file named name_function.py, I will write a simple function that takes a first and last name, and returns a full name:三相异步减速电机十大品牌
理解测试的最好⽅法是亲⾃进⾏测试。 为此,在名为name_function.py的⽂件中,我将编写⼀个简单的函数,该函数具有名字和姓⽒,并返回全名:
The function formatted_name() takes the first and the last name and combines them with a space between to form a full name. It then capitalizes the first letter of every word. To check that this code works, you need to write some code that uses this function. In names.py I will write some simple code that lets users enter their first and last names:
函数formatted_name()接受名字和姓⽒,并将其与空格之间组合起来以形成全名。 然后,将每个单词的⾸字母⼤写。 要检查此代码是否有效,您需要编写⼀些使⽤此功能的代码。 在names.py中,我将编写⼀些简单的代码,让⽤户输⼊名字和姓⽒:
This code imports formatted_name() from name_function.py and on running, allows the user to enter a series of first and last names and shows the formatted full names.
该代码从name_function.py中导⼊formatted_name()并在运⾏时导⼊,允许⽤户输⼊⼀系列的名字和姓⽒,并显⽰格式化的全名。
单元测试和测试⽤例 (Unit test and Test cases)
There is a module in Python’s standard library called unittest which contains tools for testing your code. Unit testing checks if all specific parts of your function’s behavior are correct, which will make integrating them together with other parts much easier.
Python标准库中有⼀个名为unittest的模块,其中包含⽤于测试代码的⼯具。 单元测试检查功能⾏为的所有特定部分是否正确,这将使它们与其他部分的集成更加容易。
Test case is a collection of unit tests which together proves that a function works as intended, inside
a full range of situations in which that function may find itself and that it’s expected to handle. Test case should consider all possible kinds of input a function could receive from users, and therefore should include tests to represent each of these situations.怎么注册网站 个人
测试⽤例是单元测试的集合,这些单元测试共同证明⼀个功能可以按预期⼯作,并且可以在该功能可能会发现⾃⼰并有望处理的所有情况下进⾏⼯作。 测试⽤例应考虑功能可以从⽤户那⾥收到的所有可能的输⼊,因此,测试⽤例应包括代表每种情况的测试。
通过考试 (Passing a test)
Here’s a typical scenario for writing tests:
这是编写测试的典型⽅案:
First you need to create a test file. Then import the unittest module, define the testing class that inherits from
unittest.TestCase, and lastly, write a series of methods to test all the cases of your function’s behavior.
⾸先,您需要创建⼀个测试⽂件。 然后导⼊unittest模块,定义从unittest.TestCase继承的测试类,最后编写⼀系列⽅法来测试函数⾏为的所有情况。
There’s a line by line explanation below the following code:
在以下代码下有逐⾏说明:
First, you need to import a unittest and the function you want to test, formatted_name() . Then you create a class, for example NamesTestCase, that will contain tests for your formatted_name() function. This class inherits from the class unittest.TestCase.
⾸先,您需要导⼊⼀个单元测试和要测试的函数formatted_name()。 然后,创建⼀个类,例如NamesTestCase,它将包含对您的formatted_name()函数的测试。 该类继承⾃unittest.TestCase类。
NamesTestCase contains a single method that tests one part of formatted_name() . You can call this method
test_first_last_name().
NamesTestCase包含⽤于测试formatted_name()的⼀部分的单个⽅法。 您可以调⽤此⽅法test_first_last_name()。
Remember that every method that starts with “test_” will be run automatically when you run test_name_function.py.
请记住,当您运⾏test_name_function.py时,所有以“ test_”开头的⽅法都将⾃动运⾏。
Within test_first_last_name() test method, you call the function you want to test and store a return value. In this example we are going to call formatted_name() with the arguments “pete” and “seeger” , and store the result in the resulting variable.
在test_first_last_name()测试⽅法中,调⽤要测试的函数并存储返回值。 在此⽰例中,我们将使⽤参数“ pete”和“ seeger”调⽤formatted_name(),并将结果存储在结果变量中。
In the last line we will use the assert method. The assert method verifies that a result you received matches the result you expected to receive. And in this case we know that formatted_name() function will return full name with capitalized first letters, so we expect the result “Pete Seeger”. To check this, the unittest’s assertEqual() method is being used.
在最后⼀⾏,我们将使⽤assert⽅法。 assert⽅法验证您收到的结果是否与预期收到的结果匹配。 在这种情况下,我们知道
formatted_name()函数将返回全名,⾸字母⼤写,因此我们期望结果为“ Pete Seeger”。 为了检查这⼀点,使⽤了unittest的assertEqual()⽅法。
self.assertEqual(result, “Pete Seeger”)
This line basically means: Compare the value in the resulting variable with “Pete Seeger” and if they are equal it’s OK, but if they are not let me know.
该⾏的基本含义是:将结果变量中的值与“ Pete Seeger”进⾏⽐较,如果它们相等,则可以,但是如果不告诉我。
On running test_name_function.py you are expected to get a OK meaning that the test has passed.
在运⾏test_name_function.py时,您将获得⼀个OK,表⽰测试已通过。
Ran 1 test in 0.001s
OK
测试失败 (Failing a test)
To show you what a failing test looks like I’m going to modify a formatted_name() function by including a new middle name argument.
为了向您展⽰测试失败的样⼦,我将通过添加⼀个新的中间名参数来修改formatted_name()函数。
So I’m going to rewrite the function to look like this:
因此,我将重写该函数,使其看起来像这样:
This version of formatted_name() will work for people with middle names, but when you test it you will see that the function is broken for people who don’t have a middle name.
这个版本的formatted_name()适⽤于具有中间名的⼈,但是在测试它时,您会发现该功能对于没有中间名的⼈⽽⾔是⽆效的。
So when you run the test_name_function.py you will get the output that looks something like this:
因此,当您运⾏test_name_function.py时,您将获得如下所⽰的输出:
ErrorTraceback (most recent call last):
File “test_name_function.py”, line 7, in test_first_last_name    result = formatted_name(“pete”, “seeger”)
TypeError: formatted_name() missing 1 required positional argument: ‘middle_name’
Ran 1 test in 0.002s编程报班学多少钱
FAILED (errors=1)
In the output you will see information that will tell you all you need to know where the test fails:
在输出中,您将看到信息,告诉您所有需要知道测试失败的地⽅:
First item in the output is the Error telling you that at least one test in test case resulted in an error.
输出中的第⼀项是错误,它告诉您测试⽤例中⾄少有⼀个测试导致错误。
Next you’ll see the file and method in which the error occurred.
接下来,您将看到发⽣错误的⽂件和⽅法。
After that you will see the line in which the error occurred.
之后,您将看到发⽣错误的⾏。
And what kind of error it is, in this case we are missing 1 argument “middle_name”.
这是什么错误,在这种情况下,我们缺少1个参数“ middle_name”。
You will also see the number of run tests, the time needed for the tests to complete, and a textual message that
represents the status of the tests with number of errors that occurred.
您还将看到运⾏测试的数量,测试完成所需的时间以及⼀条⽂本消息,该⽂本消息表⽰测试的状态以及发⽣的错误数量。
测试失败时该怎么办 (What to do when the test has failed)
A passing test means the function is behaving according to what’s expected from it. However, a failing test means
there’s more fun ahead of you.
通过测试意味着该功能正在按照预期的⽅式运⾏。 但是,失败的测试意味着您还有更多的乐趣。
I’ve seen couple of programmers that prefer to change the test instead of improving the code — but don’t to that. Spend a little more time to fix the issue, as it will help you to better understand the code and save time in the long run.
我见过⼀些程序员喜欢更改测试⽽不是改进代码,但是他们并不想这么做。 花更多的时间来解决此问题,因为它可以帮助您更好地理解代码并从长远来看节省时间。
In this example, our function formatted_name() first required two parameters, and now as it is rewritten it requires one extra: a middle name. Adding a middle name to our function broke the desired behavior of it. Since the idea is not to make changes to the tests, the best solution is to make middle name optional.
在此⽰例中,我们的函数formatted_name()⾸先需要两个参数,⽽现在在重写它时,它⼜需要⼀个额外的参数:中间名。 在我们的函数中添加中间名会破坏它的预期⾏为。 由于不打算更改测试,因此最好的解决⽅案是使中间名可选。
After we do this the idea is to make the tests pass when the first and last name are used, for exampl
e “Pete Seeger”, as well as when first, last and middle names are used, for example “Raymond Red Reddington”. So let’s modify the code of formatted_name() once again:
完成此操作后,我们的想法是使测试在使⽤名字和姓⽒(例如“ Pete Seeger”)以及使⽤名字,姓⽒和中间名(例如“ Raymond Red Reddington”)时通过。 因此,让我们再次修改formatted_name()的代码:
Now the function should work for names with and without the middle name.
现在,该函数应该适⽤于带有或不带有中间名的名称。
And to make sure it still works with “Pete Seeger” run the test again:
为了确保它仍然可以与“ Pete Seeger”⼀起使⽤,请再次运⾏测试:
Ran 1 test in 0.001s
舍曲林之乎OK
And this is what I intended to show you: It’s always better to make changes to your code to fit your tests than other way around. Now the time has come to add a new test for names that do have a middle name.
这就是我想要向您显⽰的内容:更改代码以适合您的测试总是⽐其他⽅法更好。 现在该是为确实具有中间名称的名称添加新测试的时候了。
添加新测试 (Adding new tests)
Write a new method to the NamesTestCase class that will test for middle names:
将新⽅法写⼊NamesTestCase类,以测试中间名:
After you run the test, both tests should pass:
运⾏测试后,两个测试都应通过:
Ran 2 tests in 0.001s
OK
Bra gjort!
胸罩gjort!
Well done!
做得好!
You have written your tests to check if the function works using names with or without a middle name. Stay tuned for part 2 where I’ll talk more about testing in Python.
您已经编写了测试以检查该函数是否使⽤带有或不带有中间名的名称。 请继续关注第2部分,在该部分中,我将详细讨论Python测试。
我可以在我的Github上到我做的这件事和其他有趣的事情: :
软件测试测试 python

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。