Skip to content

Commit

Permalink
Added DiscoverRunner for django
Browse files Browse the repository at this point in the history
  • Loading branch information
cansarigol committed Jun 4, 2021
1 parent 8d585b8 commit f8183c3
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lint:
poetry run nox --sessions lint

test:
poetry run nox --sessions check && poetry run nox --sessions test
poetry run nox --sessions check test django_test

celery:
celery -A tasks worker --loglevel=info
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ In order to user `pdbr` with pytest `--pdb` flag, add `addopts` setting in your
[pytest]
addopts: --pdbcls=pdbr:RichPdb
```
### Shell
## Django DiscoverRunner
To being activated the pdb in Django test, change `TEST_RUNNER` like below. Unlike Django (since you are not allowed to use for smaller versions than 3), pdbr runner can be used for version 1.8 and subsequent versions.

```
TEST_RUNNER = "pdbr.runner.PdbrDiscoverRunner"
```
![](/images/image10.png)
## Shell
Running `pdbr` command in terminal starts an `IPython` terminal app instance. Unlike default `TerminalInteractiveShell`, the new shell uses pdbr as debugger class instead of `ipdb`.
#### %debug magic sample
![](/images/image9.png)
Expand Down
Binary file added images/image10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ def check(session, reuse_venv=True):
def test(session, reuse_venv=True):
session.install("pytest", "pytest-cov", "rich", "icecream", "prompt_toolkit")
session.run("pytest", "--cov-report", "term-missing", "--cov=pdbr", "tests")


@nox.session
@nox.parametrize("django", ["1.8", "1.11", "2.0", "2.2", "3.0"])
def django_test(session, django, reuse_venv=True):
session.install(f"django=={django}", "rich", "pytest")
session.run("python", "runtests.py")
3 changes: 2 additions & 1 deletion pdbr/_pdbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import re
from pdb import Pdb

from icecream import ic
from rich import box
from rich._inspect import Inspect
from rich.console import Console
Expand Down Expand Up @@ -254,6 +253,8 @@ def do_icecream(self, arg):
Icecream print.
"""
try:
from icecream import ic

val = self._getval(arg)
ic.configureOutput(prefix="🍦 |> ")
self._print(ic.format(arg, val))
Expand Down
30 changes: 30 additions & 0 deletions pdbr/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest

from django.test.runner import DebugSQLTextTestResult, DiscoverRunner

from pdbr.__main__ import RichPdb, post_mortem


class PDBRDebugResult(unittest.TextTestResult):
_pdbr = RichPdb()

def addError(self, test, err):
super().addError(test, err)
self._print(test, err)

def addFailure(self, test, err):
super().addFailure(test, err)
self._print(test, err)

def _print(self, test, err):
self.buffer = False
self._pdbr.message(f"\n{test}")
self._pdbr.error(f"{err[0].__name__}: {err[1]}")
post_mortem(err[2])


class PdbrDiscoverRunner(DiscoverRunner):
def get_resultclass(self):
if self.debug_sql:
return DebugSQLTextTestResult
return PDBRDebugResult
18 changes: 9 additions & 9 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import sys

import django
from django.conf import settings
from django.test.utils import get_runner

if __name__ == "__main__":
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.tests_django.test_settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(["tests"])
sys.exit(bool(failures))
Empty file added tests/tests_django/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions tests/tests_django/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = "fake-key"
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}

TEST_RUNNER = "pdbr.runner.PdbrDiscoverRunner"
6 changes: 6 additions & 0 deletions tests/tests_django/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.test import TestCase


class DjangoTest(TestCase):
def test_runner(self):
self.assertEqual("foo", "foo")

0 comments on commit f8183c3

Please sign in to comment.