Pynguin: Automated Unit Test Generation for Python
Stephan Lukasczyk 0000-0002-0092-3476 University of PassauPassauGermany and Gordon Fraser 0000-0002-4364-6595 University of PassauPassauGermany
Abstract
Automated unit test generation is a well-known methodology aiming to reduce the developers’ effort of writing tests manually. Prior research focused mainly on statically typed programming languages like Java. In practice, however, dynamically typed languages have received a huge gain in popularity over the last decade. This introduces the need for tools and research on test generation for these languages, too. We introduce Pynguin, an extendable test-generation framework for Python, which generates regression tests with high code coverage. Pynguin is designed to be easily usable by practitioners; it is also extensible to allow researchers to adapt it for their needs and to enable future research. We provide a demo of Pynguin at https://youtu.be/UiGrG25Vts0; further information, documentation, the tool, and its source code are available at https://www.pynguin.eu.
中文速览
动态类型语言 Python 长期缺乏自动化单元测试生成工具,而现有工具要么针对 Java 等静态类型语言,要么需要开发者手动介入才能产出测试用例。Pynguin 正是为填补这一空白而设计的开源框架:它以搜索算法(如 DynaMOSA、MIO 等)为核心驱动,自动分析 Python 模块的类、函数和参数类型注解,构建"测试集群",随机或依据类型约束生成测试输入,再借助字节码插桩测量行覆盖率和分支覆盖率,最终输出符合 PyTest 风格的回归测试代码,并通过变异测试自动添加断言。在 17 个开源库的 118 个模块上,Pynguin 平均达到约 68% 的分支覆盖率,且引入类型信息后覆盖率中位数最多可提升 2.7 个百分点。这项工作的意义在于,它首次为 Python 这一主流动态类型语言提供了一个可扩展、易使用的自动化测试生成框架,既帮助开发者降低手写测试的负担,也为学界探索动态类型语言下的新测试技术提供了可复用的研究平台。
原文 arXiv:2202.05218;中英对照 + 大白话阅读 https://aha.fim.ai/paper/2202.05218v1