Unit testing is a stage of testing where the smallest segment of code that can be tested in isolation from the rest of the system - often a class - is tested. Unit tests are typically written as executable code, often in a format provided by a unit testing framework such as pytest for Python. Creating unit tests is a time and effort-intensive process with many repetitive, manual elements. To illustrate how AI can support unit testing, this chapter introduces the concept of search-based unit test generation. This technique frames the selection of test input as an optimization problem - we seek a set of test cases that meet some measurable goal of a tester - and unleashes powerful metaheuristic search algorithms to identify the best possible test cases within a restricted timeframe. This chapter introduces two algorithms that can generate pytest-formatted unit tests, tuned towards coverage of source code statements. The chapter concludes by discussing more advanced concepts and gives pointers to further reading for how artificial intelligence can support developers and testers when unit testing software.
翻译:单位测试是一个测试阶段,在这个阶段中,可以在与系统其他部分分离的情况下进行最小部分的代码(通常是一个等级)测试。单位测试通常以可执行代码的形式写成,通常采用Python测试等单位测试框架提供的格式。创建单位测试是一个时间和精力密集的过程,有许多重复的手工元素。为了说明AI如何支持单位测试,本章引入了基于搜索的单位测试生成概念。这一技术将选择测试输入作为一个优化问题来设置。我们寻求一系列符合测试者某些可测量目标的测试案例,并启用了强大的计量经济学搜索算法,以确定在限制的时间框架内可能的最佳测试案例。本章引入了两种算法,可以生成以测试格式格式显示单位测试的单位测试测试,并调整到源代码说明的覆盖范围。本章最后讨论了更先进的概念,并让指针进一步阅读人工智能如何在单位测试软件时支持开发者和测试者。