pytest-pktbin 0.1.0__tar.gz

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.
@@ -0,0 +1,217 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+
204
+ # Ruff stuff:
205
+ .ruff_cache/
206
+
207
+ # PyPI configuration file
208
+ .pypirc
209
+
210
+ # Marimo
211
+ marimo/_static/
212
+ marimo/_lsp/
213
+ __marimo__/
214
+
215
+ # Streamlit
216
+ .streamlit/secrets.toml
217
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pktbin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,117 @@
1
+ Metadata-Version: 2.5
2
+ Name: pytest-pktbin
3
+ Version: 0.1.0
4
+ Summary: pytest fixtures for pktbin: a new capture for each test, to check the traffic it sends.
5
+ Project-URL: Homepage, https://pktbin.com
6
+ Project-URL: Documentation, https://pktbin.com/docs/
7
+ Author: Pktbin
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: network,packet capture,pcap,pytest,testing
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Framework :: Pytest
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
+ Classifier: Topic :: Software Development :: Testing
22
+ Classifier: Typing :: Typed
23
+ Requires-Python: >=3.11
24
+ Requires-Dist: pktbin<0.2,>=0.1
25
+ Requires-Dist: pytest>=8
26
+ Description-Content-Type: text/markdown
27
+
28
+ # pytest-pktbin
29
+
30
+ pytest fixtures for [pktbin](https://pypi.org/project/pktbin/). Each test gets a new capture.
31
+ The test sends requests to the capture, then examines them.
32
+
33
+ Python 3.11 or later.
34
+
35
+ ```bash
36
+ pip install pytest-pktbin
37
+ ```
38
+
39
+ ## Example
40
+
41
+ ```python
42
+ import httpx
43
+
44
+
45
+ def test_sends_a_user_agent(pktbin_capture):
46
+ httpx.get(pktbin_capture.url("/hello"))
47
+
48
+ details = pktbin_capture.wait_for_request_details(path="/hello")
49
+ assert details.request["user-agent"].startswith("python-httpx/")
50
+ ```
51
+
52
+ ## Fixtures
53
+
54
+ | Fixture | Scope | Value |
55
+ |---|---|---|
56
+ | `pktbin_capture` | test | A new `CaptureSession`. |
57
+ | `pktbin_async_capture` | test | A new `AsyncCaptureSession`, for async tests. |
58
+ | `pktbin_client` | session | The `Client` that creates the captures. |
59
+
60
+ In async tests, use `pktbin_async_capture`. Its methods run on the test's event loop, so the
61
+ loop can send traffic while a method waits. A blocking method stops the loop. The fixture works
62
+ with pytest-asyncio and with anyio.
63
+
64
+ ## Failed tests
65
+
66
+ When a test fails, the report lists the first 10 requests of its capture, each with its link,
67
+ then the link to the capture:
68
+
69
+ ```text
70
+ ---------------------------- pktbin ----------------------------
71
+ GET https://<id>.de.pktbin.net/hello -> 503 Service Unavailable https://pktbin.com/ca1b2c3/requests?request=11
72
+ GET https://<id>.de.pktbin.net/hello -> 200 OK https://pktbin.com/ca1b2c3/requests?request=23
73
+ pktbin_capture: https://pktbin.com/ca1b2c3
74
+ ```
75
+
76
+ A new capture is private. The links open it only in a browser that has the owner token. The
77
+ request links never have the token. To show the capture's owner link, which has the token, set
78
+ `pktbin_owner_links`:
79
+
80
+ ```ini
81
+ [pytest]
82
+ pktbin_owner_links = true
83
+ ```
84
+
85
+ Do not set it if other people can read your test logs. Anyone with the owner link can change
86
+ and delete the capture.
87
+
88
+ ## Which captures are kept
89
+
90
+ A failed test's capture is kept until it expires, so its links work. A passing test's capture
91
+ is deleted when the test ends: nobody sees it, and it may hold tokens that the test sent. To
92
+ keep them all, set `pktbin_retention`:
93
+
94
+ ```ini
95
+ [pytest]
96
+ pktbin_retention = all
97
+ ```
98
+
99
+ If a delete fails, for example because pktbin does not answer, the plugin warns. The test
100
+ does not fail.
101
+
102
+ ## Configuration
103
+
104
+ | Setting | Meaning |
105
+ |---|---|
106
+ | `pktbin_owner_links` (ini) | `true` shows the owner link of a failed test's capture. |
107
+ | `pktbin_retention` (ini) | Which captures are kept: `failed` (the default), `all` or `none`, as pytest's `tmp_path_retention_policy`. |
108
+
109
+ ## Rate limit
110
+
111
+ pktbin limits how many captures one address creates in one minute. If a test suite
112
+ creates more, the client waits until pktbin allows the next capture, and issues a
113
+ `PktbinRateLimitWarning`. The suite is slower, but it does not fail.
114
+
115
+ ## License
116
+
117
+ MIT
@@ -0,0 +1,90 @@
1
+ # pytest-pktbin
2
+
3
+ pytest fixtures for [pktbin](https://pypi.org/project/pktbin/). Each test gets a new capture.
4
+ The test sends requests to the capture, then examines them.
5
+
6
+ Python 3.11 or later.
7
+
8
+ ```bash
9
+ pip install pytest-pktbin
10
+ ```
11
+
12
+ ## Example
13
+
14
+ ```python
15
+ import httpx
16
+
17
+
18
+ def test_sends_a_user_agent(pktbin_capture):
19
+ httpx.get(pktbin_capture.url("/hello"))
20
+
21
+ details = pktbin_capture.wait_for_request_details(path="/hello")
22
+ assert details.request["user-agent"].startswith("python-httpx/")
23
+ ```
24
+
25
+ ## Fixtures
26
+
27
+ | Fixture | Scope | Value |
28
+ |---|---|---|
29
+ | `pktbin_capture` | test | A new `CaptureSession`. |
30
+ | `pktbin_async_capture` | test | A new `AsyncCaptureSession`, for async tests. |
31
+ | `pktbin_client` | session | The `Client` that creates the captures. |
32
+
33
+ In async tests, use `pktbin_async_capture`. Its methods run on the test's event loop, so the
34
+ loop can send traffic while a method waits. A blocking method stops the loop. The fixture works
35
+ with pytest-asyncio and with anyio.
36
+
37
+ ## Failed tests
38
+
39
+ When a test fails, the report lists the first 10 requests of its capture, each with its link,
40
+ then the link to the capture:
41
+
42
+ ```text
43
+ ---------------------------- pktbin ----------------------------
44
+ GET https://<id>.de.pktbin.net/hello -> 503 Service Unavailable https://pktbin.com/ca1b2c3/requests?request=11
45
+ GET https://<id>.de.pktbin.net/hello -> 200 OK https://pktbin.com/ca1b2c3/requests?request=23
46
+ pktbin_capture: https://pktbin.com/ca1b2c3
47
+ ```
48
+
49
+ A new capture is private. The links open it only in a browser that has the owner token. The
50
+ request links never have the token. To show the capture's owner link, which has the token, set
51
+ `pktbin_owner_links`:
52
+
53
+ ```ini
54
+ [pytest]
55
+ pktbin_owner_links = true
56
+ ```
57
+
58
+ Do not set it if other people can read your test logs. Anyone with the owner link can change
59
+ and delete the capture.
60
+
61
+ ## Which captures are kept
62
+
63
+ A failed test's capture is kept until it expires, so its links work. A passing test's capture
64
+ is deleted when the test ends: nobody sees it, and it may hold tokens that the test sent. To
65
+ keep them all, set `pktbin_retention`:
66
+
67
+ ```ini
68
+ [pytest]
69
+ pktbin_retention = all
70
+ ```
71
+
72
+ If a delete fails, for example because pktbin does not answer, the plugin warns. The test
73
+ does not fail.
74
+
75
+ ## Configuration
76
+
77
+ | Setting | Meaning |
78
+ |---|---|
79
+ | `pktbin_owner_links` (ini) | `true` shows the owner link of a failed test's capture. |
80
+ | `pktbin_retention` (ini) | Which captures are kept: `failed` (the default), `all` or `none`, as pytest's `tmp_path_retention_policy`. |
81
+
82
+ ## Rate limit
83
+
84
+ pktbin limits how many captures one address creates in one minute. If a test suite
85
+ creates more, the client waits until pktbin allows the next capture, and issues a
86
+ `PktbinRateLimitWarning`. The suite is slower, but it does not fail.
87
+
88
+ ## License
89
+
90
+ MIT
@@ -0,0 +1,104 @@
1
+ [project]
2
+ name = "pytest-pktbin"
3
+ dynamic = ["version"]
4
+ description = "pytest fixtures for pktbin: a new capture for each test, to check the traffic it sends."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ authors = [{ name = "Pktbin" }]
8
+ requires-python = ">=3.11"
9
+ dependencies = [
10
+ "pktbin>=0.1,<0.2",
11
+ "pytest>=8",
12
+ ]
13
+ keywords = ["pytest", "packet capture", "pcap", "network", "testing"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Framework :: Pytest",
17
+ "Intended Audience :: Developers",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3 :: Only",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Programming Language :: Python :: 3.13",
24
+ "Programming Language :: Python :: 3.14",
25
+ "Topic :: Software Development :: Testing",
26
+ "Typing :: Typed",
27
+ ]
28
+
29
+ [project.urls]
30
+ Homepage = "https://pktbin.com"
31
+ Documentation = "https://pktbin.com/docs/"
32
+
33
+ [project.entry-points.pytest11]
34
+ pktbin = "pytest_pktbin"
35
+
36
+ [dependency-groups]
37
+ dev = [
38
+ "poethepoet>=0.48.0",
39
+ "pytest-asyncio>=1.4.0",
40
+ "ruff>=0.17.0",
41
+ "ty>=0.0.86",
42
+ ]
43
+
44
+ [tool.uv.sources]
45
+ pktbin = { path = "../pktbin", editable = true }
46
+
47
+ [build-system]
48
+ requires = ["hatchling", "uv-dynamic-versioning"]
49
+ build-backend = "hatchling.build"
50
+
51
+ [tool.hatch.build.targets.wheel]
52
+ only-include = ["pytest_pktbin.py"]
53
+
54
+ [tool.hatch.build.targets.sdist]
55
+ only-include = ["pytest_pktbin.py", "README.md", "LICENSE"]
56
+
57
+ # Released as tags of its folder's path and version, e.g.
58
+ # clients/python/pytest-pktbin/v0.1.0, as Go tags modules in a repository's folders.
59
+ [tool.uv-dynamic-versioning]
60
+ pattern-prefix = "clients/python/pytest-pktbin/"
61
+
62
+ [tool.hatch.version]
63
+ source = "uv-dynamic-versioning"
64
+
65
+ [tool.ruff.lint]
66
+ # What ruff checked by default before 0.16, which turned on hundreds more rules.
67
+ select = ["E4", "E7", "E9", "F"]
68
+
69
+ [tool.pytest.ini_options]
70
+ markers = [
71
+ "integration: marks tests that call a sensor's API (deselect with '-m \"not integration\"')"
72
+ ]
73
+
74
+ [tool.poe.tasks.format]
75
+ help = "Run all formatting tools on the code base"
76
+ sequence = ["fix-ruff", "format-ruff"]
77
+
78
+ [tool.poe.tasks.fix-ruff]
79
+ help = "Run ruff fixer on code base"
80
+ cmd = "ruff check . --fix-only"
81
+
82
+ [tool.poe.tasks.format-ruff]
83
+ help = "Run ruff format on the code base"
84
+ cmd = "ruff format ."
85
+
86
+ [tool.poe.tasks.style]
87
+ help = "Validate code style"
88
+ cmd = "ruff format --check ."
89
+
90
+ [tool.poe.tasks.lint]
91
+ help = "Run linting tools on the code base"
92
+ cmd = "ruff check ."
93
+
94
+ [tool.poe.tasks.types]
95
+ help = "Run the type checker"
96
+ cmd = "ty check ."
97
+
98
+ [tool.poe.tasks.test]
99
+ help = "Run unit and feature tests"
100
+ cmd = "pytest"
101
+
102
+ [tool.poe.tasks.check]
103
+ help = "Run all checks on the code base"
104
+ sequence = ["style", "lint", "types", "test"]
@@ -0,0 +1,180 @@
1
+ """pytest fixtures that give each test a new pktbin capture.
2
+
3
+ pytest loads this plugin in each run. The fixtures import pktbin only when a test uses
4
+ them, because the import takes about 0.1 s.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from collections.abc import Generator, Iterator
10
+ from typing import TYPE_CHECKING
11
+
12
+ import pytest
13
+
14
+ if TYPE_CHECKING:
15
+ from pktbin import AsyncCaptureSession, Capture, CaptureSession, Client, Request
16
+
17
+ _CAPTURES = ("pktbin_capture", "pktbin_async_capture")
18
+
19
+ # Which tests' captures are kept at teardown, as pytest's tmp_path_retention_policy.
20
+ _RETENTIONS = ("all", "failed", "none")
21
+
22
+ # Each test's reports so far, by phase, which teardown reads its outcome from.
23
+ _REPORTS = pytest.StashKey[dict[str | None, pytest.TestReport]]()
24
+
25
+
26
+ def pytest_addoption(parser: pytest.Parser) -> None:
27
+ parser.addini(
28
+ "pktbin_owner_links",
29
+ "Show the capture's owner link, which holds its token, when a test fails. "
30
+ "The default shows the view link only.",
31
+ type="bool",
32
+ default=False,
33
+ )
34
+ parser.addini(
35
+ "pktbin_retention",
36
+ "Which tests' captures are kept when they end: 'failed' (the default) deletes "
37
+ "those of tests that passed, 'all' keeps every capture, 'none' deletes every "
38
+ "capture.",
39
+ default="failed",
40
+ )
41
+
42
+
43
+ def pytest_configure(config: pytest.Config) -> None:
44
+ retention = config.getini("pktbin_retention")
45
+ if retention not in _RETENTIONS:
46
+ raise pytest.UsageError(
47
+ f"pktbin_retention must be one of {', '.join(_RETENTIONS)}, not {retention!r}"
48
+ )
49
+
50
+
51
+ def _retire(request: pytest.FixtureRequest, client: Client, capture: Capture) -> None:
52
+ """Deletes a test's capture unless `pktbin_retention` keeps it. A delete that fails
53
+ warns: it doesn't fail the test."""
54
+ import warnings
55
+
56
+ from pktbin import CaptureSession, PktbinError
57
+
58
+ retention = request.config.getini("pktbin_retention")
59
+ reports = request.node.stash.get(_REPORTS, {})
60
+ failed = any(report.failed for report in reports.values())
61
+ if retention == "all" or (retention == "failed" and failed):
62
+ return
63
+ try:
64
+ CaptureSession(client, capture).delete()
65
+ except PktbinError as e:
66
+ warnings.warn(f"Couldn't delete capture {capture.id}: {e}", stacklevel=1)
67
+
68
+
69
+ @pytest.fixture(scope="session")
70
+ def pktbin_client() -> Iterator[Client]:
71
+ """The client that creates the captures. All tests of a session use it."""
72
+ from pktbin import Client
73
+
74
+ with Client() as client:
75
+ yield client
76
+
77
+
78
+ @pytest.fixture
79
+ def pktbin_capture(
80
+ request: pytest.FixtureRequest, pktbin_client: Client
81
+ ) -> Iterator[CaptureSession]:
82
+ """A new capture for the test.
83
+
84
+ Send traffic to its `urls.ingest`, then examine the traffic with the methods of the
85
+ capture. If the test fails, the report shows the link to the capture, which is kept.
86
+ A passing test's capture is deleted, unless `pktbin_retention` says otherwise.
87
+ """
88
+ capture = pktbin_client.create_capture()
89
+ yield capture
90
+ _retire(request, pktbin_client, capture.metadata)
91
+
92
+
93
+ @pytest.fixture
94
+ def pktbin_async_capture(
95
+ request: pytest.FixtureRequest, pktbin_client: Client
96
+ ) -> Iterator[AsyncCaptureSession]:
97
+ """A new capture with async methods, for async tests.
98
+
99
+ The methods run on the event loop of the test, so the loop can send traffic while a
100
+ method waits. A blocking method stops the loop. The fixture itself is not async, so
101
+ it works with pytest-asyncio and with anyio. Its capture is kept or deleted as
102
+ `pktbin_capture`'s is.
103
+ """
104
+ import anyio
105
+ import httpx
106
+
107
+ from pktbin import AsyncCaptureSession, AsyncClient
108
+
109
+ capture = pktbin_client.create_capture()
110
+ # The client keeps no connections, because they would belong to the test's event
111
+ # loop, which can be closed before teardown.
112
+ client = AsyncClient(limits=httpx.Limits(max_keepalive_connections=0))
113
+ yield AsyncCaptureSession(client, capture.metadata)
114
+ anyio.run(client.aclose)
115
+ _retire(request, pktbin_client, capture.metadata)
116
+
117
+
118
+ # The most requests that the report of a failed test lists for each capture.
119
+ _LISTED = 10
120
+
121
+
122
+ def _describe(request: Request) -> str:
123
+ """The method, target and status of a request, on one line."""
124
+ status = request.status
125
+ answer = f"{status.code} {status.text}" if status else "no answer"
126
+ return f"{request.method} {request.target} -> {answer}"
127
+
128
+
129
+ def _request_lines(
130
+ client: Client, capture: CaptureSession | AsyncCaptureSession
131
+ ) -> list[str]:
132
+ """A line for each of the capture's first requests, with its link."""
133
+ from pktbin import CaptureSession
134
+
135
+ # Read with the session's client, which is sync, for an async capture too.
136
+ requests = CaptureSession(client, capture.metadata).requests()
137
+ lines = [
138
+ f" {_describe(request)} {capture.link(request)}"
139
+ for request in requests[:_LISTED]
140
+ ]
141
+ if len(requests) > _LISTED:
142
+ lines.append(f" and {len(requests) - _LISTED} more")
143
+ return lines
144
+
145
+
146
+ @pytest.hookimpl(wrapper=True)
147
+ def pytest_runtest_makereport(
148
+ item: pytest.Item, call: pytest.CallInfo[None]
149
+ ) -> Generator[None, pytest.TestReport, pytest.TestReport]:
150
+ """Adds the capture's requests and its link to the report of a failed test."""
151
+ report = yield
152
+ item.stash.setdefault(_REPORTS, {})[report.when] = report
153
+ if report.failed:
154
+ funcargs = getattr(item, "funcargs", {})
155
+ owner = item.config.getini("pktbin_owner_links")
156
+ client = funcargs.get("pktbin_client")
157
+ for name in _CAPTURES:
158
+ capture = funcargs.get(name)
159
+ if capture is None:
160
+ continue
161
+ lines: list[str] = []
162
+ if client is not None and not _sensor_failed(call):
163
+ try:
164
+ lines = _request_lines(client, capture)
165
+ except Exception:
166
+ # The link alone still helps.
167
+ lines = []
168
+ link = (capture.owner_url if owner else None) or capture.urls.view
169
+ report.sections.append(("pktbin", "\n".join([*lines, f"{name}: {link}"])))
170
+ return report
171
+
172
+
173
+ def _sensor_failed(call: pytest.CallInfo[None]) -> bool:
174
+ """Whether the test failed because the sensor did not answer. Then listing the
175
+ requests would only wait for the client's retries."""
176
+ if call.excinfo is None:
177
+ return False
178
+ from pktbin import PktbinConnectionError, PktbinTimeoutError
179
+
180
+ return call.excinfo.errisinstance((PktbinConnectionError, PktbinTimeoutError))