pytest-neon 2.0.0__py3-none-any.whl → 2.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pytest_neon/__init__.py CHANGED
@@ -9,7 +9,7 @@ from pytest_neon.plugin import (
9
9
  neon_engine,
10
10
  )
11
11
 
12
- __version__ = "2.0.0"
12
+ __version__ = "2.1.0"
13
13
  __all__ = [
14
14
  "NeonBranch",
15
15
  "neon_branch",
pytest_neon/plugin.py CHANGED
@@ -56,6 +56,17 @@ DEFAULT_BRANCH_EXPIRY_SECONDS = 600
56
56
  _MIGRATIONS_NOT_DEFINED = object()
57
57
 
58
58
 
59
+ def _get_xdist_worker_id() -> str:
60
+ """
61
+ Get the pytest-xdist worker ID, or "main" if not running under xdist.
62
+
63
+ When running tests in parallel with pytest-xdist, each worker process
64
+ gets a unique ID (gw0, gw1, gw2, etc.). This is used to create separate
65
+ branches per worker to avoid database state pollution between parallel tests.
66
+ """
67
+ return os.environ.get("PYTEST_XDIST_WORKER", "main")
68
+
69
+
59
70
  def _get_schema_fingerprint(connection_string: str) -> tuple[tuple[Any, ...], ...]:
60
71
  """
61
72
  Get a fingerprint of the database schema for change detection.
@@ -527,6 +538,12 @@ def _neon_branch_for_reset(
527
538
  session, avoiding issues with Python's module caching (e.g., SQLAlchemy
528
539
  engines created at import time would otherwise point to stale branches).
529
540
 
541
+ Parallel Test Support (pytest-xdist):
542
+ When running tests in parallel with pytest-xdist, each worker gets its
543
+ own branch. This prevents database state pollution between tests running
544
+ concurrently on different workers. The worker ID is included in the
545
+ branch name suffix (e.g., "-test-gw0", "-test-gw1").
546
+
530
547
  Smart Migration Detection:
531
548
  This fixture implements a cost-optimization strategy:
532
549
 
@@ -558,15 +575,21 @@ def _neon_branch_for_reset(
558
575
  # Assume migrations changed something to be safe
559
576
  schema_changed = True
560
577
 
578
+ # Get worker ID for parallel test support
579
+ # Each xdist worker gets its own branch to avoid state pollution
580
+ worker_id = _get_xdist_worker_id()
581
+ branch_suffix = f"-test-{worker_id}"
582
+
561
583
  # Only create a child branch if migrations actually modified the schema
562
- if schema_changed:
584
+ # OR if we're running under xdist (each worker needs its own branch)
585
+ if schema_changed or worker_id != "main":
563
586
  yield from _create_neon_branch(
564
587
  request,
565
588
  parent_branch_id_override=_neon_migration_branch.branch_id,
566
- branch_name_suffix="-test",
589
+ branch_name_suffix=branch_suffix,
567
590
  )
568
591
  else:
569
- # No schema changes - reuse the migration branch directly
592
+ # No schema changes and not parallel - reuse the migration branch directly
570
593
  # This saves creating an unnecessary branch
571
594
  yield _neon_migration_branch
572
595
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytest-neon
3
- Version: 2.0.0
3
+ Version: 2.1.0
4
4
  Summary: Pytest plugin for Neon database branch isolation in tests
5
5
  Project-URL: Homepage, https://github.com/ZainRizvi/pytest-neon
6
6
  Project-URL: Repository, https://github.com/ZainRizvi/pytest-neon
@@ -489,16 +489,26 @@ The `neon_branch_readwrite` fixture uses Neon's branch restore API to reset data
489
489
 
490
490
  This is similar to database transactions but at the branch level.
491
491
 
492
- ## Limitations
492
+ ## Parallel Test Execution (pytest-xdist)
493
493
 
494
- ### Parallel Test Execution
494
+ This plugin supports parallel test execution with [pytest-xdist](https://pytest-xdist.readthedocs.io/). Each xdist worker automatically gets its own isolated branch.
495
495
 
496
- This plugin sets the `DATABASE_URL` environment variable, which is process-global. This means it is **not compatible with pytest-xdist** or other parallel test runners that run tests in the same process.
496
+ ```bash
497
+ # Run tests in parallel with 4 workers
498
+ pip install pytest-xdist
499
+ pytest -n 4
500
+ ```
501
+
502
+ **How it works:**
503
+ - Each xdist worker (gw0, gw1, gw2, etc.) creates its own branch
504
+ - Branches are named with the worker ID suffix (e.g., `-test-gw0`, `-test-gw1`)
505
+ - Workers run tests in parallel without database state interference
506
+ - All branches are cleaned up after the test session
497
507
 
498
- If you need parallel execution, you can:
499
- - Use `neon_branch.connection_string` directly instead of relying on `DATABASE_URL`
500
- - Run with `pytest-xdist --dist=loadfile` to keep modules in separate processes
501
- - Run tests serially (default pytest behavior)
508
+ **Cost implications:**
509
+ - Running with `-n 4` creates 4 branches (one per worker) plus the migration branch
510
+ - Choose your parallelism level based on your Neon plan's branch limits
511
+ - Each worker's branch is reset after each test using the fast reset operation (~0.5s)
502
512
 
503
513
  ## Troubleshooting
504
514
 
@@ -0,0 +1,8 @@
1
+ pytest_neon/__init__.py,sha256=69SoGL2ciE38uvrXpEQPXiBFDkbWct7hl_k9MmswcSU,398
2
+ pytest_neon/plugin.py,sha256=rb9CgOqle-sQHHoyFO5xDZ5wzrHx_4BLygiVywEYGbM,34949
3
+ pytest_neon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ pytest_neon-2.1.0.dist-info/METADATA,sha256=ebdenaxT8v4GQeepZCglp04mb92iwJxp1PMnnS-9Nqs,18734
5
+ pytest_neon-2.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ pytest_neon-2.1.0.dist-info/entry_points.txt,sha256=5U88Idj_G8-PSDb9VF3OwYFbGLHnGOo_GxgYvi0dtXw,37
7
+ pytest_neon-2.1.0.dist-info/licenses/LICENSE,sha256=aKKp_Ex4WBHTByY4BhXJ181dzB_qYhi2pCUmZ7Spn_0,1067
8
+ pytest_neon-2.1.0.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- pytest_neon/__init__.py,sha256=Ti_bjX7CgEZjtaY_uoNnkSvRUXnEw4MkfsbFie_K6Mo,398
2
- pytest_neon/plugin.py,sha256=9VH7h2UYp0AoJj3FagpP6Xqtd1_qqkcuS0sLF-cqYpo,33877
3
- pytest_neon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- pytest_neon-2.0.0.dist-info/METADATA,sha256=bQ7y0oOcymrho3AY9NhHRFT1FNM4jPK27rRFh217FN4,18386
5
- pytest_neon-2.0.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
- pytest_neon-2.0.0.dist-info/entry_points.txt,sha256=5U88Idj_G8-PSDb9VF3OwYFbGLHnGOo_GxgYvi0dtXw,37
7
- pytest_neon-2.0.0.dist-info/licenses/LICENSE,sha256=aKKp_Ex4WBHTByY4BhXJ181dzB_qYhi2pCUmZ7Spn_0,1067
8
- pytest_neon-2.0.0.dist-info/RECORD,,