Automatic Generation of Smell-free Unit Tests
João L. Afonso, José Campos · 2023
Automated test generation tools, such as EvoSuite, typically aim to generate tests that maximize code coverage and do not adequately consider non-coverage aspects that may be relevant for developers, e.g., test’s code quality. Hence, automatically generated tests are often affected by test-specific bad programming practices, i.e., test smells, that may hinder the quality of the test’s source code and, ultimately, the quality of the code under test. Although EvoSuite uses secondary criteria and a post-processing procedure to optimize non-coverage aspects and improve the readability of the tests, it does not explicitly consider the usage of good programming practices. Thus, in this paper, we propose a novel approach to assist EvoSuite’s search algorithm in generating smell-free tests out of the box. To this aim, we first compile a set of 54 test smell metrics from several sources. Secondly, we systematically identify 30 smells that do not affect the tests generated by EvoSuite and eight smells that cannot be automatically computed. Thirdly, we incorporate the remaining 16 test smells as metrics into EvoSuite and empirically identify that only 14 smells affect the tests generated by the tool (e.g., Indirect Testing). Fourthly, we describe and integrate an approach to optimize test smell metrics into EvoSuite. Finally, we conduct an empirical study to (i) understand to what extend EvoSuite’s default mechanisms leads to the generation of fewer smelly tests. (ii) to assess whether our approach leads to the generation of fewer smelly tests. And (iii) how our approach affects the coverage and fault detection effectiveness of the generated tests. Our results report that our approach can generate 8.58% fewer smelly tests without significantly compromising their coverage or fault detection effectiveness.