Source-linked AI summary

Pynguin: Automated Unit Test Generation for Python

Stephan Lukasczyk, Gordon Fraser

arXiv:2202.05218v1cs.SE

TL;DR

Automated test generation has focused mainly on statically typed languages, despite the growing importance of dynamically typed Python and the challenges of generating tests without reliable type information. Pynguin is an extensible Python framework that uses search-based generation and available type annotations to create regression tests for high coverage. Its initial evaluation reached up to 68.0% branch coverage, while type information improved coverage by up to 2.7 percentage points depending on the algorithm.

  • Problem

    Prior test-generation research mainly targeted statically typed languages, while dynamically typed Python lacks sufficient automated support and often omits information needed for test generation.

  • Method

    Pynguin is an extensible Python framework that uses search-based test generation and incorporates available type annotations into its test-generation process.

  • Results

    Up to 68.0% average branch coverage was achieved on 118 Python modules from 17 open-source libraries, with type information improving coverage by up to 2.7 percentage points depending on the algorithm.

  • Takeaways & Limitations

    Pynguin provides a framework for automated regression-test generation in Python that can support both practical use and further research on dynamically typed languages.

  • Takeaways & Limitations

    Coverage may not adequately reflect type-related effects, and missing type information can prevent Pynguin from instantiating the correct objects.

Abstract

from arXiv · show

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.

1 INTRODUCTION

Prior automated test-generation research focused mainly on statically typed languages, while Python’s popularity and dynamic typing created a need for dedicated tooling. Pynguin addresses this gap with an extensible Python framework that generates regression tests, achieving substantial branch coverage in an initial evaluation.

  • Motivation: Prior research predominantly targeted statically typed languages such as Java, leaving dynamically typed languages comparatively under-supported.The paper identifies this focus as a limitation as Python and other dynamically typed languages have gained popularity.
  • Motivation: Python’s growing use, particularly in data science and machine learning, increases the need for automated tools supporting software testing and code quality.The paper describes automated test-generation support as lacking in the Python toolbox.
  • Motivation: Dynamic typing complicates test generation because programs often lack variable-type information, while runtime type changes, object modification, and coercions make static derivation difficult.These challenges have limited prior work to specific aspects or restricted scenarios, sometimes requiring manual developer effort.
  • Contribution: Pynguin is an extensible open-source Python framework that uses search-based generation to produce regression tests targeting high code coverage.Its extensibility supports new coverage criteria and test-generation algorithms for researchers and practitioners.
  • Evaluation: 68.0% average branch coverage was achieved on 118 Python modules from 17 open-source libraries in the initial evaluation.The evaluation presents this as evidence that automated regression-test generation is feasible for a dynamically typed language.
  • Evaluation: Up to 2.7 percentage points of median coverage improvement resulted from incorporating type information, depending on the test-generation algorithm.This result is reported over all used projects.

2 TEST GENERATION WITH PYNGUIN

Pynguin is an extensible Python framework that analyzes modules, generates coverage-oriented tests, and supports type-aware test construction despite Python's dynamic typing. Its modular design supports multiple generation algorithms, regression assertions, command-line use, and extensions.

  • Test-generation process: Pynguin analyzes a Python module, builds a test cluster containing its classes, functions, methods, and parameters, and uses this information during test generation.The framework extracts module information before constructing test cases.
  • Test-generation process: Pynguin constructs test cases by selecting target functions, satisfying parameter requirements backwards, and recursively generating required objects.With type annotations, it generates assignments such as var = <num>; complex objects are built through involved methods' parameters.
  • Search and coverage: Users can choose DynaMOSA, MIO, MOSA, random, Whole Suite, or Whole Suite with archive, while line or branch coverage serves as an optimization goal.Additional coverage variants require corresponding fitness functions.
  • Regression assertions: Pynguin optionally generates regression assertions using mutation testing by comparing tests' observed values on the original module and its mutants.A customized MutPy version supports mutant generation and execution.
  • Usage: Pynguin is primarily used as a command-line application, supports a rudimentary API, and requires users to acknowledge risks from arbitrary random inputs.The command-line interface requires project, module, and output paths.
  • Dynamic typing: Type annotations help Pynguin select objects satisfying function and method parameters, but missing type information can prevent valid and useful test generation.The paper identifies type inference as an open research problem and motivates Pynguin's extensibility around it.
  • Extensibility: Pynguin is modular and extensible: researchers can replace stages, add generation algorithms, export styles, and incorporate external type-inference techniques.New algorithms can be added by extending TestGenerationStrategy and implementing generate_tests.

3 EVALUATION

The evaluation measures Pynguin's branch coverage across 118 Python modules and compares six generation algorithms over a 600-second budget. Search-based techniques outperform random generation, with DynaMOSA achieving the highest mean coverage.

  • Experimental setup: Pynguin was evaluated on 118 modules, running version 0.17.0 30 times per module and configuration with a 600 s timeout.The repeated runs were intended to reduce the influence of randomness.
  • Experimental setup: The evaluation measured mean branch coverage over the 600 s generation period.Coverage development was compared across configurations over time.
  • Results: Search-based techniques outperform the random algorithm, while DynaMOSA achieves the highest mean branch coverage among the evaluated configurations.The five search-based algorithms show only small differences.
  • Results: 68.0 % mean branch coverage is achieved by DynaMOSA, compared with 63.6 % for Random and 66.9 % for Whole Suite.Other reported values are 67.0 % for MIO, 67.8 % for MOSA, and 67.5 % for Whole Suite with archive.

4 CONCLUSIONS

Pynguin is introduced as an automated unit test generation framework for Python that supports developers and enables extensions and research. Its open-source availability is intended to foster further research and practical applicability.

  • Pynguin is an automated unit test generation framework designed to support developers implementing unit tests manually.
  • Its command-line interface provides flexibility for users, while its modular design enables extensions and research for dynamically typed languages.
  • The authors provide Pynguin as open source to foster further research and applicability in practice.
Loading 2202.05218v1…