API reference¶
This page documents the full public API of behave-pool. Each module is
documented with its classes, methods, and usage examples.
Package¶
behave_pool
¶
behave-pool: parallel test execution for Behave BDD via native ITestRunner.
Public API:
from behave_pool import ParallelRunner
# Register in behave.ini:
# [behave.runners]
# parallel = behave_pool:ParallelRunner
# Then run:
# behave --runner=parallel --parallel 4 --parallel-scheme feature features/
ParallelRunner
¶
Bases: Runner
Coordinator that dispatches work units to worker processes.
When config.parallel <= 1 it falls back to the standard Behave
sequential runner. Otherwise it plans, dispatches, and collects
results from N worker processes.
Source code in behave_pool/runner.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
ParallelRunner¶
The coordinator that orchestrates parallel feature execution. Extends
behave.runner.Runner and implements Behave's ITestRunner interface.
from behave_pool import ParallelRunner
from behave.configuration import Configuration
config = Configuration(["--parallel", "4", "features/"])
runner = ParallelRunner(config)
failed = runner.run() # True if any test failed
behave_pool.runner
¶
ParallelRunner: coordinator that orchestrates parallel feature execution.
ParallelRunner
¶
Bases: Runner
Coordinator that dispatches work units to worker processes.
When config.parallel <= 1 it falls back to the standard Behave
sequential runner. Otherwise it plans, dispatches, and collects
results from N worker processes.
Source code in behave_pool/runner.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
Configuration¶
ConfigSnapshot is a picklable snapshot of Behave's Configuration that can
be safely sent to worker processes via spawn.
from behave_pool.config import ConfigSnapshot, snapshot_config
snapshot = snapshot_config(config)
print(snapshot.parallel) # 4
print(snapshot.parallel_scheme) # "feature"
print(snapshot.base_dir) # "features"
behave_pool.config
¶
Parallel configuration options for behave-pool.
ConfigSnapshot
dataclass
¶
Picklable snapshot of essential Configuration fields for worker processes.
The full behave Configuration contains non-picklable objects (file handles, reporters). This snapshot captures only the fields needed by WorkerRunner to execute features.
Source code in behave_pool/config.py
add_parallel_options
¶
Add parallel-related attributes to a Configuration instance.
Maps behave's config.jobs (from --parallel/--jobs) to
config.parallel and ensures config.parallel_scheme exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Configuration
|
Behave Configuration instance to augment. |
required |
Source code in behave_pool/config.py
snapshot_config
¶
Create a picklable snapshot from a Configuration instance.
Source code in behave_pool/config.py
WorkUnit¶
A frozen dataclass representing a single unit of test work dispatched to a worker process.
from behave_pool.work_unit import WorkUnit
from behave_pool.config import ConfigSnapshot
unit = WorkUnit(
id="feature:features/login.feature",
config=ConfigSnapshot(base_dir="features", steps_dir="steps"),
feature_path="features/login.feature",
tags=["serial"],
)
print(unit.is_serial) # True
behave_pool.work_unit
¶
WorkUnit: a single unit of test work to be dispatched to a worker.
WorkUnit
dataclass
¶
A single unit of test work for parallel dispatch.
A WorkUnit represents either a whole feature file or a single scenario (identified by line number) within a feature file. It carries a picklable ConfigSnapshot so that workers can execute independently after being spawned via multiprocessing.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier, e.g. "feature:login.feature" or "scenario:login.feature:12". |
config |
ConfigSnapshot
|
Picklable ConfigSnapshot with essential configuration. |
feature_path |
str | None
|
Path to the .feature file. |
scenario_line |
int | None
|
Line number of the scenario within the feature file. None when the work unit represents an entire feature. |
tags |
list[str]
|
Tags associated with the scenario or feature. |
Source code in behave_pool/work_unit.py
is_serial
property
¶
True if this work unit is tagged with @serial.
Serial work units are executed sequentially after all parallel work units have completed.
WorkerResult¶
The outcome of executing a WorkUnit in a worker process.
from behave_pool.result import WorkerResult
result = WorkerResult(
worker_id=0,
work_unit_id="feature:features/login.feature",
failed=False,
duration=1.23,
)
print(result.failed) # False
print(result.duration) # 1.23
behave_pool.result
¶
WorkerResult: the outcome of executing a WorkUnit in a worker process.
WorkerResult
dataclass
¶
Result of a worker executing a single WorkUnit.
Produced by WorkerRunner.run_work_unit() and sent back to the coordinator via the result queue for aggregation.
Attributes:
| Name | Type | Description |
|---|---|---|
worker_id |
int
|
Identifier of the worker process that produced this result. |
work_unit_id |
str
|
ID of the WorkUnit that was executed. |
failed |
bool
|
True if any scenario or step in the work unit failed. |
duration |
float
|
Wall-clock execution time in seconds. |
report_path |
str | None
|
Path to the temporary JSON report file, or None if no report was written. |
undefined_steps |
list[str]
|
List of undefined step text patterns encountered. |
error |
str | None
|
Error message if the worker process crashed, None otherwise. |
Source code in behave_pool/result.py
TimingStore¶
Loads and saves historical work unit durations as JSON for LPT balancing.
from pathlib import Path
from behave_pool.timing import TimingStore
store = TimingStore(path=Path(".behave-pool-timing.json"))
store.load()
print(store.get_duration("feature:features/login.feature")) # 1.23
store.update("feature:features/login.feature", 1.45)
store.save_if_changed() # True if file was written
behave_pool.timing
¶
TimingStore: persist historical work unit durations for LPT balancing.
TimingStore
¶
Load and save historical work unit durations as JSON.
The file format is a simple mapping of work unit IDs to durations in seconds::
{"feature:login.feature": 1.23, "feature:checkout.feature": 0.45}
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path to the JSON timing file. |
Source code in behave_pool/timing.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
get_duration
¶
Return the stored duration for a work unit, or 0.0 if unknown.
load
¶
Load timing data from the JSON file.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict mapping work unit IDs to durations in seconds. |
dict[str, float]
|
Returns an empty dict if the file is missing or corrupt. |
Source code in behave_pool/timing.py
save
¶
Write timing data to the JSON file atomically with indent=2.
Writes to a temporary file in the same directory and then atomically replaces the target file, preventing corruption if the process crashes during a write.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, float]
|
Dict mapping work unit IDs to durations in seconds. |
required |
Source code in behave_pool/timing.py
save_if_changed
¶
Save data only if it differs from what was loaded.
Returns:
| Type | Description |
|---|---|
bool
|
True if the file was written, False if no changes were detected. |
Source code in behave_pool/timing.py
update
¶
Insert or update the duration for a work unit.
Non-finite values (inf, NaN) are rejected because they produce non-standard JSON and break save_if_changed equality checks (NaN != NaN).
Source code in behave_pool/timing.py
WorkUnitIterator¶
Strategy pattern for generating WorkUnit objects from parsed features.
from behave_pool.iterator import WorkUnitIterator
iterator = WorkUnitIterator.for_scheme(
scheme="feature",
features=features,
config=config,
)
for unit in iterator.iterate():
print(unit.id, unit.feature_path)
behave_pool.iterator
¶
Strategy pattern for iterating work units from Behave features.
FeatureIterator
¶
Bases: WorkUnitIterator
Generate one WorkUnit per feature file.
Each WorkUnit contains an isolated deep copy of the Configuration so that workers can execute independently without shared mutable state.
Source code in behave_pool/iterator.py
iterate
¶
Yield one WorkUnit per feature.
Tags are collected from both the feature and its scenarios.
If any scenario has the serial tag, the work unit is
marked serial so it runs in the serial phase.
Source code in behave_pool/iterator.py
WorkUnitIterator
¶
Bases: ABC
Abstract strategy for generating WorkUnits from parsed features.
Source code in behave_pool/iterator.py
for_scheme
staticmethod
¶
Factory: return the iterator for the given parallel scheme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scheme
|
str
|
"feature" or "scenario". |
required |
features
|
list[Feature]
|
Parsed Behave Feature objects. |
required |
config
|
Configuration
|
Coordinator's Configuration (will be deep-copied per unit). |
required |
Returns:
| Type | Description |
|---|---|
WorkUnitIterator
|
A WorkUnitIterator instance for the requested scheme. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If scheme is not recognised. |
NotImplementedError
|
If scheme is "scenario" (not yet implemented). |
Source code in behave_pool/iterator.py
Worker¶
WorkerRunner executes work units inside an isolated worker process.
WorkerProcess wraps multiprocessing.Process for lifecycle management.
These classes are used internally by ParallelRunner and are not typically
instantiated directly by end users.
behave_pool.worker
¶
WorkerRunner and WorkerProcess for parallel test execution.
WorkerProcess
¶
Wrapper around multiprocessing.Process for consuming WorkUnits.
Each WorkerProcess runs _worker_run_loop in a separate process, consuming WorkUnits from a JoinableQueue and producing WorkerResults in a result Queue.
Source code in behave_pool/worker.py
is_alive
¶
join
¶
start
¶
Launch the worker process.
Source code in behave_pool/worker.py
terminate
¶
Forcefully terminate the worker process.
Should only be called after join(timeout=...) returns and
is_alive() is still True, as a last resort to avoid
indefinite hangs from stuck workers.
Source code in behave_pool/worker.py
WorkerRunner
¶
Bases: ModelRunner
Runner that executes work units in an isolated worker process.
Lifecycle
- setup() — once at worker start (load hooks, steps, before_all).
- run_work_unit(unit) — called per work unit.
- teardown() — once at worker end (after_all, close formatters).
Source code in behave_pool/worker.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
collect_result
¶
load_hooks
¶
Load environment hooks from the environment file.
Source code in behave_pool/worker.py
load_step_definitions
¶
Load step definitions from the steps directory.
Source code in behave_pool/worker.py
run_work_unit
¶
Execute a single work unit and return the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
WorkUnit
|
The WorkUnit to execute. |
required |
Returns:
| Type | Description |
|---|---|
WorkerResult
|
WorkerResult with timing, failure status, and report path. |
Source code in behave_pool/worker.py
setup
¶
Load hooks, step definitions, create Context, run before_all.
Source code in behave_pool/worker.py
teardown
¶
Run after_all hooks and close formatters.
Safe to call even if setup() did not complete: skips hooks and formatters that were never initialised.