变异函数 python

We need to kill the mutants — no, I’m not a villain from the X-Men comics. I’m a software engineer who wants to improve unit tests.

我们需要杀死这些突变体-不,我不是X战警漫画中的反派。 我是一位软件工程师,希望改善单元测试。

In this article you will learn what mutation testing is and how it can help you to write better tests. The examples are for Python, but the concepts hold in general and in the end I have a list of tools in other languages.

在本文中,您将学习什么是突变测试以及它如何帮助您编写更好的测试。 这些示例是针对Python的,但是这些概念是通用的,最后我有了其他语言的工具列表。

为什么我们需要进行突变测试? (Why do we need mutation testing?)

Unit tests have the issue that it’s unclear when your tests are good enough. Do you cover the important edge cases? How do you test the quality of your unit tests?

单元测试存在一个问题,即您的测试何时足够好尚不清楚。 您涵盖重要的案例吗? 您如何测试单元测试的质量?

Typical mistakes are slight confusions. Accessing list[i] instead of list[i-1] , letting the loop run for i < n instead of i <= n , initializing a variable with None instead of the empty string. There are a lot of those slight changes which are usually just called “typos” or “off-by-one” mistakes. When I make them, I often didn’t think about the part thoroughly enough.

典型的错误是轻微的混乱。 访问list[i]而不是list[i-1] ,让循环在i < n而不是i <= n ,并使用None而不是空字符串初始化变量。 许多细微的变化通常被称为“ 错别字 ”或“ 一对一 ”错误。 当我制作它们时,我常常没有充分地考虑零件。

Mutation testing tests your unit tests. The key idea is to apply those minor changes and run the unit tests that could fail. If a unit test fails, the mutant was killed. Which is what we want. It shows that this kind of off-by-one mistake cannot happen with our test suite. Of course, we assume that the unit tests themselves are correct or at worst incomplete. Hence you can see a mutation test as an alternative to test coverage. In contrast to test coverage, the mutation testing toolkit can directly show you places and types of mistakes you would not cover right now.

变异测试会测试您的单元测试 。 关键思想是应用这些较小的更改并运行可能失败的单元测试。 如果单元测试失败,则突变体被杀死。 这就是我们想要的。 它表明,我们的测试套件不可能发生这种一次性的错误。 当然,我们假设单元测试本身是正确的,或者最糟糕的是不完整。 因此,您可以看到变异测试可以替代测试覆盖率。 与测试覆盖率相反,变异测试工具包可以直接向您显示您现在不会覆盖的错误的位置和类型。

有哪些突变测试工具? (Which mutation testing tools are there?)

There are a couple of tools like cosmic-ray, but Anders Hovmöller did a pretty amazing job by creating mutmut. As of August 2020, mutmut is the best library for Python to do mutation testing.

有一对夫妇像宇宙射线的工具,但安德斯Hovmöller通过创建mutmut做了非常了不起的工作。 截至2020年8月,mutmut是Python进行突变测试的最佳库。

To run the examples in this article, you have to install mutmut:

要运行本文中的示例,您必须安装mutmut :

pip install mutmut

In other languages, you might want to try these:

使用其他语言,您可能需要尝试以下方法:

  • C / C++: mull

    C / C ++: 仔细考虑

  • Java: PIT (GitHub)

    Java: PIT ( GitHub )

  • JavaScript: Stryker

    JavaScript: Stryker

  • PHP: Infection (formerly called humbug)

    PHP: 感染 (以前称为humbug)

  • Ruby: mutant

    Ruby: 突变体

  • Rust: mutagen

    Rust: 诱变剂

  • Swift: muter

    斯威夫特: 静音

为什么分支和线路覆盖范围不够? (Why isn’t branch and line coverage enough?)

It is pretty easy to get to a high line coverage by creating bad tests. For example, take this code:

通过创建不良测试,很容易获得较高的覆盖率。 例如,使用以下代码:

def fibonacci(n: int) -> int:    """Get the n-th Fibonacci number, starting with 0 and 1."""    a, b = 0, 1    for _ in range(n):        a, b = b, a + b    return b  # BUG! should be a!def test_fibonacci():    fibonacci(10)

This smoke test already adds some value as it makes sure that things are not crashing for a single input. However, it would not find any logic bug. There is an assert statement missing. This pattern can quickly drive up the line coverage up to 100%, but you are then still lacking good tests.

此烟雾测试已经增加了一些价值,因为它可以确保单个输入不会崩溃。 但是,它不会发现任何逻辑错误。 缺少assert语句。 这种模式可以快速将线路覆盖率提高到100%,但是您仍然缺乏良好的测试。

A mutation test cannot be fooled as easily. It would mutate the code and, for example, initialize b with 0 instead of 1:

突变测试不容易被愚弄。 它将使代码突变,例如,将b初始化为0而不是1:

- a, b = 0, 1+ a, b = 0, 0

The test would still succeed and thus the mutant would survive. Which means the mutation testing framework would complain that this line was not properly tested. In other words:

测试仍将成功,因此突变体将存活。 这意味着变异测试框架会抱怨此行未正确测试。 换一种说法:

Mutation testing provides another way to get a more rigid line coverage. It can still not guarantee that a tested line is correct, but it can show you potential bugs that your current test suite would not detect.

变异测试提供了获得更严格的线覆盖率的另一种方法。 它仍然不能保证测试的行是正确的,但是可以显示当前测试套件无法检测到的潜在错误。

创建突变体! (Create the mutants!)

As always, I use my small mpu library as an example. At the moment, it has a 99% branch and 99% line coverage.

与往常一样,我以我的小型mpu库为例。 目前,它具有99%的分支和99%的线路覆盖率。

$ mutmut run- Mutation testing starting -These are the steps:1. A full test suite run will be made to make sure we   can run the tests successfully and we know how long   it takes (to detect infinite loops for example)2. Mutants will be generated and checkedResults are stored in .mutmut-cache.Print found mutants with `mutmut results`.Legend for output:												

变异函数 python_使用Python进行变异测试相关推荐

  1. just函数 python_在python里写Monad

    这段时间写微信小程序看见callback hell非常炸毛,而且确实我不熟悉js的各种衍生工具(async/await, promise),想着python的coroutine是yield模改出来的, ...

  2. just函数python_提升 Python 性能 Numba 与 Cython

    ? "Python猫" ,一个值得加星标的公众号花下猫语:最近,读者微信群里又频繁聊到了 Python 的性能问题,这真是老生常谈了.我想起自己曾收藏过几篇关于如何提升性能的文章, ...

  3. 匿名函数python_基于python内置函数与匿名函数详解

    内置函数 Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() d ...

  4. node函数 python_成为Python大牛必须要掌握的高端语法(附链接代码)

    来源:机器学习算法与Python实战 本文约3000字,建议阅读5分钟 必要掌握的高端语法助你成为Python大牛. 这是stackoverflow上一个关于python中yield用法的帖子,这里翻 ...

  5. ans函数python_#12 Python函数

    前言 矩形的面积 S = ab,只要知道任一矩形的的长和宽,就可以带入上式求得面积.这样有什么好处呢?一个公式,适用于全部矩形,一个公式,重复利用,减少了大脑的记忆负担.像这类用变量代替不变量的思想在 ...

  6. isdigit函数python_在Python中处理字符串之isdigit()方法的使用

    在Python中处理字符串之isdigit()方法的使用 更新时间:2015年05月18日 12:06:29 投稿:goldensun 这篇文章主要介绍了在Python中处理字符串之isdigit() ...

  7. 插值、平稳假设、本征假设、变异函数、基台、块金、克里格、线性无偏最优…地学计算概念及公式推导

    插值.平稳假设.本征假设.变异函数.基台.块金.克里格.线性无偏最优-地学计算概念及公式推导 1 引言 2 空间插值 3 几个重要假设 3.1 平稳假设 3.2 二阶平稳性假设 3.3 本征假设 3. ...

  8. ArcGIS实验教程——实验三十四:ArcGIS地统计分析全解(直方图、正态QQ图、趋势分析、voronoi图、半变异函数、协方差云)

    ArcGIS实验视频教程合集:<ArcGIS实验教程从入门到精通>(附配套实验数据)> 文章目录 一.地统计分析的基本原理 二.地统计分析的工作流程 三.探索性空间数据分析工具 1. ...

  9. .describe() python_【python小白上路系列】函数

    第二个周末持续加班了,体力严重透支. 在本章中,你将学习编写函数 .函数是带名字的代码块,用于完成具体的工作. 要执行函数定义的特定任务,可调用该函数.需要在程序中多次执行同一项任务时,你无需反复编写 ...

最新文章

  1. mysql 设置 0、1 用什么数据类型_MySQL索引的基本使用
  2. ES 的分布式架构原理能说一下么?
  3. CentOS 7核心安装及基本配置
  4. 【知识发现】隐语义模型LFM算法python实现(二)
  5. 网工路由基础(1)静态路由
  6. Jenkins添加从节点相关配置
  7. sftp服务器同步文件到本地,服务器之间通过sftp的方式同步文件,并入库到本地数据的表中...
  8. 一行代码搞定 FTP 服务
  9. 软件质量保证划重点期末复习总结
  10. __RESTRICT修改为__RRSTRICT,程序闪退。
  11. 一个nginx 502问题解决方案
  12. okhttp离线缓存_Android改造OkHttp离线缓存
  13. ASP.NET基础教程-Server对象
  14. 脱硫系统中的烟气挡板门选电动型、气动型?还是电动液压型?快别纠结了……
  15. 中国4G网络走近大融合时代
  16. 聚集理论派+实践派大咖,与您一起剖析CI/CD的那些事儿 | 在线研讨会《如何实现集中管理、灵活高效的CI/CD》
  17. AOSP build 系统简介
  18. ATeam社区(牛客网项目第七章)
  19. git报错Push to origin/master was rejected的解决办法
  20. 微信8年,这几个冷知识你都知道吗?想必知道的人也不多吧

热门文章

  1. opensuse x64下编译Ice源码(以编译c++为例)
  2. Azure恢复服务-使用Windows Backup恢复文件
  3. [js高手之路]this知多少
  4. Python 开发者在迁移到 Go(lang) 时需要知道哪些事?
  5. IT新潮关键词汇整理
  6. 精通Android开发 1
  7. CSS行高——line-height
  8. SPI模式下MCU对SD卡的控制及操作命令
  9. 聚集索引和填充因子fill factor的设置,减少死锁
  10. Eclipse集成Maven插件tomcat部署 Debug jar包的源码联结