pytest-dev / pytest-dev/pytest-django

Client: testing exceptions in views (client.store_exc_info)

Open
#130 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

I want to test a view that throws an exception, and came up with the following code to make it work, so that the exception is not re-thrown by the client (code ref: https://github.com/django/django/blob/master/django/test/client.py#L421-443):

def test_foo(client):
    def store_exc_info(*args, **kwargs):
        pass
    # client.store_exc_info = lambda *args, **kwargs: True
    client.store_exc_info = store_exc_info

    client.get('/raises-500')

Given that it was not trivial to figure this out I wonder if this is the correct approach and if pytest-django could help in this regard (documentation, helper, ...)

I will look into providing a documentation update for Django itself, but want to get some feedback first.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Django client.py section linked in the issue, especially client.store_exc_info, and review how pytest-django currently documents testing views that raise exceptions. Determine whether the result should be a documentation clarification or a pytest-django helper, then document or test the agreed approach so the exception-handling behavior is clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
testing
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.