软件测试测试 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.

您刚刚编写了一段代码,并且想知道该怎么做。 您将提交拉取请求并让您的队友查看代码,还是手动测试代码? 您应该执行所有这些操作,但是要执行一个附加步骤:您需要对代码进行单元测试,以确保代码按预期工作。

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 example “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测试。

This and other fun stuff I do can be found on my Github: https://github.com/GoranAviani

我可以在我的Github上找到我做的这件事和其他有趣的事情: https : //github.com/GoranAviani

翻译自: https://www.freecodecamp.org/news/testing-in-python-c6b903eb247d/

软件测试测试 python

软件测试测试 python_Python测试简介相关推荐

  1. 测试软件测试赢在测试2:中国软件测试专家访谈录

    新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正 赢在测试2:中国软件测试专家访谈录(品专家脚印悟测试真理本书已在台湾发行) 蔡为东著 ISBN 978-7-121-20066-3 ...

  2. 软件测试质量保证与测试

    软件测试质量保证与测试 第一章 软件测试概述 1.1 软件测试背景 随着计算机技术的迅速发展和越来越广泛深入地应用于国民经济与社会生活的各个方面,软件系统的规模和复杂性与日俱增,软件的生产成本和软件中 ...

  3. 软件测试面试题:什么是α测试,β测试?

    什么是α测试,β测试? α.β.λ 常用来表示软件测试过程中的三个阶段: α 是第一阶段,一般只供内部测试使用: β 是第二个阶段,已经消除了软件中大部分的不完善之处,但仍有可能还存在缺陷和漏洞,一般 ...

  4. 软件测试基础___测试理论篇

    前言 高手,是避免失败,而不是追求成功 目录 前言 高手,是避免失败,而不是追求成功 1.为什么要写测试理论篇 2.软件测试的目标,模型及核心 3.软件测试目标案例 4.软件测试的策略 5.软件测试的 ...

  5. 【软件测试-04】Web应用程序测试的测试流程和相关理论知识;

    目录 web测试: 测试方法: 1.1界面测试: web测试的主要页面元素: 测试技术 1.2 界面测试要素: 2.1功能测试 连接: 表单提交: Cookies 验证: 功能易用性测试 2. 2.测 ...

  6. 软件测试界面测试是什么,什么是软件测试的界面测试?

    <什么是软件测试的界面测试?>由会员分享,可在线阅读,更多相关<什么是软件测试的界面测试?(4页珍藏版)>请在人人文库网上搜索. 1.精品什么是软件测试的界面测试?现在一般人都 ...

  7. 软件测试开发:常见测试类型概念

    软件测试是软件开发中非常重要的一个环节,软件测试工程师需要对每个环节进行严格把控,才能保证系统在每个阶段得以控制.下面小编就为大家详细介绍一下软件测试开发:常见测试类型概念的相关内容. 软件测试开发: ...

  8. 软件测试培训 高级测试/测试开发基本技能列表

    软件测试培训从事软件测试许多年,想必很多人都有感到迷茫不知所措的时候,人生的十字路口有很多,该如何抉择呢?有人成功转型,QA.项目管理.配置管理.当然还有技术型,性能测试.自动化测试.测试开发,而想要 ...

  9. 面向对象测试与传统软件测试有何异同,面向对象软件测试与传统测试有何区别...

    由于面向对象技术具有封装.继承.多态等新的特性,带来了传统程序设计所不存在的错误,导致原来行之有效的软件测试技术对面向对象技术开发的软件有些力不从心. 面向对象软件测试与传统软件测试有以下不同: 测试 ...

最新文章

  1. java+jsp+mysql网页制作总结(1)
  2. 《Haskell函数式编程入门》——导读
  3. ubuntu / 18.04 系统上配置 DHCP Server
  4. 404. Sum of Left Leaves 左叶子之和
  5. myeclipse写简单bbs代码_RabbitMQ实现即时通讯居然如此简单!连后端代码都省得写了?...
  6. 基础练习 十进制转十六进制 C语言
  7. figma下载_切换到Figma并在其中工作不必是火箭科学,这就是为什么
  8. PMP模拟考试系统-pmp模拟考试题库
  9. Firefox Monitor - 在线免费检测邮箱数据是否泄露
  10. 2020超星android测试,2020知到《现代物流学》免费答案超星尔雅《测试作业导入》答案公众号...
  11. 《SEM长尾搜索营销策略解密》一一2.1 起因:核心词成本过高
  12. win10 win11黑屏引导转圈开机时间过长
  13. 发那科sub_PMC功能指令之定时器TMR(SUB3)
  14. centos7安装es mysql_centos7.2安装Mysql5.7.13
  15. OSChina 周二乱弹 ——程序员在聊天中注意观察什么细节
  16. 微信小程序入门7-微信公众号设置IP白名单
  17. 直播程序源码Android10.0 导航栏和状态栏动态控制合集
  18. 深度学习小白入门教程-基础环境篇
  19. 三星android获取root权限,三星i9000 Android 2.3.3获取root权限教程
  20. 爬取招聘网站信息,并使用pyecharts和matplotlib进行简单的可视化测试

热门文章

  1. Ubuntu 16.04 UUID 开机自动挂载硬盘
  2. 安装fastdfs依赖包
  3. 190916-二级format补齐
  4. python-运算符-比较运算符
  5. 移动开发技术有哪些?
  6. 商桥物流完成达晨创投领投2.5亿元A+轮融资,创建立体化零担物流网络
  7. scala 学习笔记一 列表List
  8. python实战===生成随机数
  9. 《高质量c++/c编程指南》学习摘要
  10. TypeScript入门教程 之 Promise