A Characteristic Study of Parameterized Unit Tests in .NET Open Source Projects

Wing Lam, Siwakorn Srisakaokul, Blake Bassett, Peyman Mahdian, Tao Xie, Pratap Lakshman, Jonathan de HalleuxECOOP 2018

TL;DR

This paper presents the first comprehensive study of how developers write Parameterized Unit Tests (PUTs) in .NET open-source projects. By analyzing hundreds of PUTs and categorizing forum discussions, the authors uncover common practices, challenges, and areas for improvement, offering valuable insights for developers, tool builders, and educators.

What Problem Does This Paper Address?

Modern software development relies heavily on automated testing, and tools like Pex (now IntelliTest) can automatically generate test inputs. However, these tools often rely on "weak" test oracles that primarily check for runtime exceptions. To create "strong" test oracles that verify functional correctness, developers can use Parameterized Unit Tests (PUTs), which allow for more expressive test specifications. Despite the growing support for PUTs in frameworks like .NET and Java, there's a significant lack of understanding regarding how developers actually write and use them in real-world projects. This gap hinders the adoption and improvement of PUTs and related tools.

Methods

The study employs a multi-stage approach:

  1. Forum Post Categorization: The authors first analyzed 93 Microsoft MSDN Pex Forum posts from industrial practitioners to identify common concerns and topics related to PUTs. This helped them prioritize research questions.
  2. Empirical Study of Open Source Projects: Based on the forum insights, they conducted a characteristic study of PUTs in .NET open-source projects. This involved:
  3. Subject Collection: Identifying 77 open-source projects with at least 10 PUTs, filtering out academic or course-related projects.
  4. Analysis Tools Development: Creating custom tools using Roslyn (.NET Compiler Platform) to parse C# source files and extract metrics related to PUTs, assumptions, assertions, attributes, and code structure.
  5. Manual Inspection: Supplementing automated analysis with manual inspection for tasks like identifying design patterns and bad code smells.

Datasets

The primary dataset consists of 77 .NET open-source projects containing Parameterized Unit Tests (PUTs). From these, 11 subjects with at least 10 PUTs each were selected for detailed analysis, resulting in a total of 741 PUTs. The study also draws insights from 93 Microsoft MSDN Pex Forum posts.

Key Insights and Ideas

  • Assumptions are King for Nullness: Developers heavily rely on assumptions (like PexAssumeUnderTest and PexAssumeNotNull) to ensure parameters are not null, far more than they use assertions for the same purpose. This makes sense, as assumptions set up the test environment, while assertions verify outcomes.
  • Hard-coded Sequences are Common, but Promotable: Many PUTs still contain hard-coded method sequences to create objects. The study found that a significant portion of these could be refactored into non-primitive parameters, making tests more general and test generation tools more effective.
  • Factory Methods are Underutilized: While promoting objects to non-primitive parameters is beneficial, developers often don't provide corresponding "factory methods" to help test generation tools create diverse and useful object states.
  • Attributes for Speed, Not Coverage: Developers often use attributes like MaxRuns to speed up test generation, but increasing default values for attributes like MaxBranches rarely leads to increased code coverage.
  • PUTs Can Be Improved: Common "bad smells" like code duplication, unnecessary conditional statements, and hard-coded test data are present in PUTs, indicating opportunities for better design patterns and developer education.

Key Results

  • Assumption Usage: PexAssumeUnderTest (273 uses) and PexAssumeNotNull (211 uses) are the most frequent assumption types, highlighting a strong focus on nullness checks. 62% of assumptions perform null checks.
  • Assertion Usage: Assertions primarily focus on equality checking, with AreEqual (355 uses) and IsTrue (199 uses) being most common. Only 5% of assertions perform null checks.
  • Non-primitive Parameter Usage: On average, 59.0% of parameters in PUTs are non-primitive. However, factory methods are provided for only 17.9% of these non-primitive parameters.
  • Receiver Object Promotion: 95.7% of PUTs have receiver objects, but 49.2% of these lack a parameter for the receiver object. Of those that could be promoted, 89.4% should be promoted.
  • Bad Smells: Code duplication (55 PUTs), unnecessary conditional statements (39 PUTs), and hard-coded test data (37 PUTs) were identified as common issues.
  • Design Patterns: The "AAA" (Arrange, Act, Assert) pattern and "Parameterized Stub" pattern are prevalent.

Limitations and Future Work

  • Limited Subject Count: The study identified only 11 subjects that met the criteria for detailed analysis, which might limit the generalizability of findings.
  • Focus on Pex: The study primarily focused on PUTs written using the Pex framework. Findings might differ for other frameworks or for PUTs used without automated test generation tools.
  • Open Source vs. Proprietary: All subjects were open-source, so findings may not directly apply to proprietary software development.
  • Tooling Limitations: The analysis tools, while tested and verified, could still contain faults. External tools used for coverage measurement might also have limitations.

Future work suggested includes:

  • Debugging and compiling the remaining subjects to increase the dataset size.
  • Investigating PUTs written with other frameworks (e.g., JUnit theories).
  • Developing tools to automatically detect and fix common bad smells in PUTs, such as code duplication and unnecessary conditionals.
  • Providing better tool support for promoting receiver objects and inferring assumptions for non-primitive parameters.

Summary updated

BibTeX
@inproceedings{lam2018characteristic,
  author = {Wing Lam and Siwakorn Srisakaokul and Blake Bassett and Peyman Mahdian and Tao Xie and Pratap Lakshman and Jonathan de Halleux},
  title = {{A Characteristic Study of Parameterized Unit Tests in .NET Open Source Projects}},
  booktitle = {ECOOP},
  year = {2018},
  doi = {10.4230/lipics.ecoop.2018.5},
}
Figure