nameko-worker-postmortem
Have you ever been frustrated with the behaviour of a Nameko service in pytest using the --pdb flag?
If your Nameko service throws a worker exception, --pdb won't interrupt with a breakpoint until the exception is serialised back to the caller of the entrypoint.
nameko-worker-postmortem is a pytest plugin that inserts the tracepoint earlier, so you can see a more useful stack trace.
Example
Here's an example test that will fail:
# test.py
from nameko.web.handlers import http
class BadRequest(Exception):
pass
class Service:
name = "service"
@http("GET", "/resource")
def resource(self, request):
param = request.args.get('param')
if param == "good":
return 200, "OK"
raise BadRequest()
def test_service(container_factory, web_config, web_session):
container = container_factory(Service, web_config)
container.start()
res = web_session.get('/resource?param=bad')
assert res.status_code == 200
With --pdb
Using the --pdb flag, the breakpoint is set where the assertion fails, which isn't very helpful if you want to see the exception that the worker raised instead:
$ py.test test.py
============================= test session starts ==============================
platform darwin -- Python 3.4.6, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: /private/tmp, inifile:
plugins: nameko-2.11.0, nameko-worker-postmortem-0.0.1
collected 1 item
test.py F
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> captured log >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
containers.py 399 ERROR error handling worker <WorkerContext [service.resource] at 0x1104a77f0>:
Traceback (most recent call last):
File "/Users/mattbennett/.virtualenvs/tmp-302905cac73c0a2/lib/python3.4/site-packages/nameko/containers.py", line 391, in _run_worker
result = method(*worker_ctx.args, **worker_ctx.kwargs)
File "/private/tmp/test.py", line 14, in resource
raise BadRequest()
test.BadRequest
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
container_factory = <function container_factory.<locals>.make_container at 0x11022f620>
web_config = {'WEB_SERVER_ADDRESS': '127.0.0.1:55934'}
web_session = <nameko.testing.pytest.web_session.<locals>.WebSession object at 0x11047ac18>
def test_service(container_factory, web_config, web_session):
container = container_factory(Service, web_config)
container.start()
res = web_session.get('/resource?param=bad')
> assert res.status_code == 200
E assert 500 == 200
E + where 500 = <Response [500]>.status_code
test.py:22: AssertionError
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
[11] > /private/tmp/test.py(22)test_service()
-> assert res.status_code == 200
6 frames hidden (try 'help hidden_frames')
> /private/tmp/test.py(22)
17 def test_service(container_factory, web_config, web_session):
18 container = container_factory(Service, web_config)
19 container.start()
20
21 res = web_session.get('/resource?param=bad')
22 -> assert res.status_code == 200
With --worker-postmortem
Replacing --pdb with --worker-postmortem (or --worker-pdb) the breakpoint is inserted at where the worker exception is raised instead.
Note that -s must also be passed to disable output capturing.
$ py.test test.py --worker-postmortem -s
============================= test session starts ==============================
platform darwin -- Python 3.4.6, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: /private/tmp, inifile:
plugins: nameko-2.11.0, nameko-worker-postmortem-0.0.1
collected 1 item
test.py [1] > /private/tmp/test.py(14)resource()
-> raise BadRequest()
> /private/tmp/test.py(14)
9 @http("GET", "/resource")
10 def resource(self, request):
11 param = request.args.get('param')
12 if param == "good":
13 return 200, "OK"
14 -> raise BadRequest()
(Pdb++)
TODO
- Fix coverage collection
- Add pre-commit hooks
- Automatically disable capturing so you don't have to specify
-severy time - Attempt to make navigation
upanddownthe stack trace work as they would with apdb.set_trace()(at the moment,updoesn't take you up the stack but into thenameko-worker-postmortemcode that inserts the breakpoint.)
Metadata
Release files for nameko-worker-postmortem 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nameko_worker_postmortem-1.0.0.tar.gz | 3.7 kB | Without provenance | Details for nameko_worker_postmortem-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nameko_worker_postmortem-1.0.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for nameko_worker_postmortem-1.0.0-py2.py3-none-any.whl |
Total release size: 7.9 kB
Release files / nameko_worker_postmortem-1.0.0.tar.gz
| Download URL | nameko_worker_postmortem-1.0.0.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a68defd6dad873b8f3ccab8ca6f036e39994911c2922375271b27f5881c703e6
|
|
BLAKE2b-256 checksum How to use checksums |
f18d8c164d0bb160c95989f5e8f389cc7d64d4ab381a4d710ba738f98bd16677
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.14
|
Release files / nameko_worker_postmortem-1.0.0-py2.py3-none-any.whl
| Download URL | nameko_worker_postmortem-1.0.0-py2.py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
dd2d13e260774a5a653f4f894c2b92bbb4c382c3f28499b2e09009abc13e040d
|
|
BLAKE2b-256 checksum How to use checksums |
e26340a900c572498177e5d5dba19fc2323a913596bef9b574b37839a05f8e0c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.14
|
Security for nameko-worker-postmortem 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.