Skip to content

Commit c14dfcc

Browse files
authored
test: add tests for flask and django management cli error handling (#23)
* test: add tests for flask and django management cli error handling * fix: python2 attempts to import management as command
1 parent a8c1bbd commit c14dfcc

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed

tests/integrations/django/myapp/management/__init__.py

Whitespace-only changes.

tests/integrations/django/myapp/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.core.management.base import BaseCommand
2+
3+
4+
class Command(BaseCommand):
5+
def add_arguments(self, parser):
6+
pass
7+
8+
def handle(self, *args, **options):
9+
1 / 0

tests/integrations/django/myapp/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"django.contrib.sessions",
4949
"django.contrib.messages",
5050
"django.contrib.staticfiles",
51+
"tests.integrations.django.myapp",
5152
]
5253

5354

tests/integrations/django/test_basic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
from django.test import Client
7+
from django.core.management import execute_from_command_line
78

89
try:
910
from django.urls import reverse
@@ -79,3 +80,11 @@ def test_user_captured(client, capture_events):
7980
def test_404(client):
8081
response = client.get("/404")
8182
assert response.status_code == 404
83+
84+
85+
def test_management_command_raises():
86+
# This just checks for our assumption that Django passes through all
87+
# exceptions by default, so our excepthook can be used for management
88+
# commands.
89+
with pytest.raises(ZeroDivisionError):
90+
execute_from_command_line(["manage.py", "mycrash"])

tests/integrations/flask/test_flask.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,19 @@ def index():
324324
def test_no_errors_without_request(app):
325325
with app.app_context():
326326
capture_exception(ValueError())
327+
328+
329+
def test_cli_commands_raise(app):
330+
if not hasattr(app, "cli"):
331+
pytest.skip("Too old flask version")
332+
333+
from flask.cli import ScriptInfo
334+
335+
@app.cli.command()
336+
def foo():
337+
1 / 0
338+
339+
with pytest.raises(ZeroDivisionError):
340+
app.cli.main(
341+
args=["foo"], prog_name="myapp", obj=ScriptInfo(create_app=lambda _: app)
342+
)

0 commit comments

Comments
 (0)