变异函数 python_使用Python进行变异测试

本文介绍了如何在Python中使用变异测试方法,通过变异函数来增强单元测试的覆盖率和质量,确保代码的健壮性。
摘要由CSDN通过智能技术生成

变异函数 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:

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值