TEST2::MANUAL::ANATOMY::CONTEXT(3perl)
π NAME π
Test2::Manual::Anatomy::Context β the internals tea on the Context objects, bestie. Like, the documentation for the documentation-adjacent guts. π«Ά
πΈ DESCRIPTION πΈ
Okay so this doc is literally just here to explain how the Test2::API::Context object works. That's it. That's the whole vibe. β¨
π WHAT IS THE CONTEXT OBJECT? π
The context object is one of the key components of Test2, no cap β it's the thing that makes so many features possible that would otherwise be straight-up impossible. π¦
The flow is iconic and it never changes:
- Every test tool starts by getting a context
- ...does all its work in between
- ...and ends by releasing the context
The context instance is the primary interface for sending events to the Test2 stack. And finally β and this is the part that's lowkey critical for debugging β the context system is responsible for tracking what file and line number a tool operates on. Very main character of it. π
π₯ PRIMARY INTERFACE FOR TEST TOOLS π₯
Nearly every Test2 based tool should start by calling:
$ctx = Test2::API::context()
...to get a context object, and should end by calling:
$ctx->release()
Once a tool has its context object it can call methods on the object to send events or have other effects. Nearly everything a test tool needs to do should be done through the context object. Periodt. π
π§ TRACK FILE AND LINE NUMBERS FOR ERROR REPORTING π§
When you call Test2::API::Context a new context object will be returned. Here's the rules, bestie:
- If there is already a context object in effect (from a different point in the stack) you will get a clone of the existing one.
- If there is not already a current context then a completely new one will be generated.
When a new context is generated Test2 will determine the file name and line number for your test code β these will be used when reporting any failures. π
Typically the file and line number will be determined using caller() to look at your tools caller. The $Test::Builder::Level will be respected if detected, but β ngl β that's discouraged in favor of just using context objects at every level.
When calling Test2::API::Context() you can specify the "level => $count" arguments if you need to look at a deeper caller. π
π PRESERVE $?, $!, $^E AND $@ π
When you call Test2::API::context() the current values of $?, $!, $^E, and $@ are stored in the context object itself. Whenever the context is released the original values of these variables will be restored. This protects the variables from any side effects caused by testing tools. Protecting the babies, fr fr. π
β FINALIZE THE API STATE β
Test2::API works via a hidden singleton instance of Test2::API::Instance. That singleton has some state that is not set in stone until the last possible minute. And when is the last possible minute, you ask? It happens to be the first time a context is acquired. Iconic timing.
The state includes:
- IPC instance
- Formatter class
- Root PID
- etc.
π FIND/CREATE THE CURRENT/ROOT HUB π
Test2 has a stack of hubs, and the stack can be accessed via Test2::API::test2_stack. When you get a context it will find the current hub β and if there is no current hub then the root one will be initialized. π«Ά
π PROVIDE HOOKS π
There are hooks that run when contexts are created, found, and released. See Test2::API for details on these hooks and how to use them. Go get your hooks, bestie.
β¨ SEE ALSO β¨
Test2::Manualβ Primary index of the manual.
π SOURCE π
The source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/.
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
πΈ AUTHORS πΈ
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
okay bestie that's the whole thing, you ate it, no crumbs left π β¨
This was perl v5.44.0, dated 2026-10-17 β and that's the Test2::Manual::Anatomy::Context page, signing off, mwah π«Άπ
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms