skip to content

Scope and Nesting

JMeter's scoping rule: hierarchical elements apply to every sampler below their parent, while controllers and samplers run in tree order. It is the tool's most misunderstood behaviour.

on this pageshow

explore

questions

5

In a JMeter test plan, what decides which samplers a timer or an assertion applies to?

level: juniorimportance: must knowfreq 78%

answer

  1. Ask what the element's parent is
  2. Sibling position versus branch depth
  3. Two families: one ordered, one not
  4. The manual calls these the scoping rules

basics

~20 s

Tree position decides. Timers, assertions, listeners, config elements and pre- and post-processors are hierarchical: they apply to every sampler at or below their parent. Attach one under a single sampler and only that sampler is affected.

solid answer

~50 s

The JMeter tree holds two kinds of element. **Hierarchical** ones — listeners, config elements, pre-processors, post-processors, assertions and timers — apply to every sampler at or below their parent, and their position among their siblings is irrelevant. **Ordered** ones — controllers and samplers — run top to bottom in the order you see them. So a Response Assertion attached to one HTTP Request checks only that request; the same assertion attached to a Simple Controller checks every sampler under that controller; attached directly under the Thread Group it checks all of them. The manual's mental model is to imagine each sampler walking up the branches — to its parent, then its parent's parent, up to the Test Plan — collecting every hierarchical element it passes. Only how deep in the branch an element hangs matters, never where it sits among its siblings.

go deeper

for a junior

Recall the two families and the one-line rule: a hierarchical element applies to every sampler at or below its parent. Know that attaching it under a single sampler is how you restrict it.

for a middle

Explain the upward walk from sampler to Test Plan, and why a timer written last in a controller still applies to the samplers above it. Be able to name the hierarchical families from memory.

for a senior

Show how you audit an existing plan for over-broad scope: trace from each suspect element down to every sampler beneath its parent, and treat a Thread Group-level attachment as something future samplers will inherit too.

for a principal

Own the convention. Decide how deep the team attaches shared elements, since a high attachment is cheap to maintain but makes reach invisible to a reviewer reading the tree.

## The rule in one sentence The JMeter test tree contains elements that are **both hierarchical and ordered**, and which family an element belongs to decides how JMeter applies it. The user manual's own split is worth memorising: - **Strictly hierarchical:** listeners, config elements, post-processors, pre-processors, assertions, timers. - **Primarily ordered:** controllers and samplers. An ordered element runs when the thread reaches it, top to bottom. A hierarchical element never "runs at a position" at all — it is *collected* by every sampler underneath its parent and applied to that sampler's turn. ## The upward walk The manual gives the model that actually works: imagine each request being passed up the tree branches to its parent, then to its parent's parent, and so on to the Test Plan, collecting every hierarchical element it meets along the way. That collected set is what configures the sampler. Two consequences follow immediately. 1. **Depth decides scope, sibling position does not.** A timer written as the *last* child of a controller applies to the samplers listed above it in that controller exactly as much as to those below. The manual states it flatly: order is irrelevant for hierarchical elements. 2. **A hierarchical element attached to a sampler applies to that sampler alone.** That is the documented way to narrow one: "To restrict an assertion to a single sampler, add the assertion as a child of the sampler." ## What each attachment level buys | Attached under | In scope for | | --- | --- | | Test Plan | every sampler in every Thread Group, including setUp and tearDown groups | | A Thread Group | every sampler in that Thread Group | | A controller | every sampler that is a descendant of that controller | | A sampler | that one sampler | The same table reads as a cost table: the higher you attach, the fewer copies you maintain and the less visible the element's reach is when someone opens the plan. ## A worked tree ``` Test Plan └─ Thread Group ├─ HTTP Request "One" │ └─ Response Assertion A ├─ Simple Controller │ ├─ HTTP Request "Two" │ ├─ HTTP Request "Three" │ └─ Constant Timer T1 └─ HTTP Request "Four" ``` The requests still execute One, Two, Three, Four — that is the ordered family at work. Assertion A checks only request One. Timer T1 pauses before requests Two and Three even though it is written after both of them, and it does nothing to One or Four. Nothing about T1's line number in the tree changes any of that. ## Where people get this wrong The most common mistake is reading the tree like a script and assuming a hierarchical element takes effect "from here downwards" among its siblings. Engineers then drag a timer or an extractor above the samplers they want it to affect, see no change, and conclude the element is broken. The element was already in scope; moving it among siblings was never going to alter that. The second mistake runs the other way: attaching something at Thread Group level because it is convenient, then being surprised that it also decorates health-check samplers, static asset fetches and anything a colleague adds to the group later. Scope in JMeter is inherited by everything below, including elements that do not exist yet. A third trap is listeners. The manual notes that listeners can be added anywhere, including directly under the Test Plan, and that they collect data only from elements at or below their level. A listener parked under one sampler reports one sampler's rows, which is rarely what someone reaching for a results file wants. ## How to check a plan 1. Pick the sampler you care about and trace the path from it up to the Test Plan. 2. At every node on that path, list its hierarchical children — those, and only those, are in scope. 3. Do the reverse for anything you suspect is over-broad: from the element, look at its parent and enumerate every sampler beneath that parent. 4. If the answer is "more samplers than I meant", move the element down a level rather than reordering it. That trace is the whole rule. Almost every "JMeter is ignoring my element" and "why is this header on every request" report resolves to a mis-read of it.

  • If a hierarchical element's position among its siblings is irrelevant, when does tree order matter at all?
    For the ordered family. Controllers and samplers are processed in the order they appear, so moving a sampler changes when it runs. Order also breaks ties inside one hierarchical type: when several timers or several assertions are in the same scope, JMeter processes them in the order they appear in the tree.
  • Where would you attach a listener so it records every sampler in the plan?
    Directly under the Test Plan. Listeners are hierarchical and collect data only from elements at or below their own level, so a plan-level listener sees every Thread Group's samplers, while one attached under a single sampler records only that sampler's results.

Think of each sampler walking from its own leaf up to the root of the tree, collecting a stamp from every parent it passes. What it ends up carrying depends on which branch it climbed, not on where the stamps were sitting on the desk.

saying these in an interview costs you the question

  • Says an element applies only to samplers listed after it
  • Moves a timer above the samplers to make it take effect
  • Treats controllers and samplers as hierarchical like timers
  • Attaches a listener under one sampler and expects whole-plan results
  • Thinks scope is set in the element's own GUI rather than by placement
open as a page

A JMeter Thread Group holds a Constant Timer and one sampler under it holds another. What pause runs?

level: middleimportance: must knowfreq 60%

basics

~20 s

Both timers are in that sampler's scope, so JMeter adds their delays together and sleeps once for the total before running it. Every other sampler in the Thread Group waits only for the Thread Group timer.

open as a page

In JMeter, what happens when two HTTP Header Managers are in one sampler's scope?

level: middleimportance: should knowfreq 47%

basics

~20 s

JMeter merges them into one header list for that sampler. Where both define the same header name, the manager nearer the sampler keeps its value, and the outer one contributes only the names the inner one does not set.

open as a page

Across a team's JMeter plans, at which tree level would you standardise attaching shared elements?

level: principalimportance: should knowfreq 35%

basics

~20 s

No single level is right, so split by element kind: broad defaults high, session managers exactly once per Thread Group, request-specific elements on the request. The trade is maintenance cost against how visible an element's reach is.

open as a page