JMeter Test Fragment
Learn how to use Test Fragments in JMeter to modularize and reuse components throughout a test plan, reducing duplication and improving maintainability.
Mark
Performance Testing Expert
JMeter enables rapid construction of test plans, typically requiring a Thread Group, Sampler, and Listener. However, reusing the same components across multiple test scenarios creates duplication. Test Fragments solve this problem by allowing developers to modularize and reuse components throughout a test plan.
Key Concept
Test Fragments execute exclusively through Module Controllers or Include Controllers, enabling “code blocks” that run on demand rather than automatically during test execution.
Implementation Steps
Setting up a Test Fragment
- Create a Simple Controller at the top level of the test plan to serve as a logical container
- Add an HTTP Request Sampler within the Simple Controller
- These components remain dormant until called
Test Plan
├── Test Fragment
│ └── Simple Controller (APIs)
│ └── HTTP Request (Authentication)
└── Thread Group
└── Module Controller (references Test Fragment)
Executing the Fragment
- Create a Thread Group in the main test plan
- Add a Module Controller beneath the Thread Group
- Configure the Module Controller to reference the Simple Controller from the Test Fragment
- Upon execution, the Module Controller invokes the Fragment’s components
Best Practices
Naming conventions matter significantly. Developers should assign unique, descriptive names to fragments and their constituent components:
- Fragment name: “APIs”
- Controller name: “Authentication”
- Sampler names: “Login Request”, “Token Validation”
This approach makes it clear which components are being referenced when using Module Controllers.
Benefits
Test Fragments provide several advantages:
- Reduce duplication: Write common functionality once, use it everywhere
- Improve maintainability: Update in one place, changes propagate automatically
- Manage file sizes: Keep test plan files organized and manageable
- Precise control: Execute components in the exact order needed
- Logical organization: Group related functionality together
Example Structure
Test Plan
├── Test Fragment
│ ├── Simple Controller (Authentication)
│ │ ├── HTTP Request (Get Token)
│ │ └── HTTP Request (Validate Token)
│ └── Simple Controller (User Operations)
│ ├── HTTP Request (Get User Profile)
│ └── HTTP Request (Update User)
├── Thread Group (Scenario 1)
│ ├── Module Controller → Authentication
│ └── Module Controller → User Operations
└── Thread Group (Scenario 2)
└── Module Controller → Authentication
Further Reading
Tags: