Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output full diffs in Django unit tests?

When I use assertEqual() with two dictionaries in REPL, it shows me a diff, e.g.:

>>> import unittest
>>> class A(unittest.TestCase):
...   pass
... 
>>> a = A()
>>> d1 = dict(zip(range(10), range(1000000, 1000010)))
>>> d2 = dict(zip(range(3, 13), range(1000003, 1000013)))
>>> a.assertEqual(d1, d2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/unittest/case.py", line 820, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.5/unittest/case.py", line 1111, in assertDictEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/usr/lib/python3.5/unittest/case.py", line 665, in fail
    raise self.failureException(msg)
AssertionError: {0: 1000000, 1: 1000001, 2: 1000002, 3: 10[73 chars]0009} != {3: 1000003, 4: 1000004, 5: 1000005, 6: 10[76 chars]0012}
- {0: 1000000,
-  1: 1000001,
-  2: 1000002,
-  3: 1000003,
? ^

+ {3: 1000003,
? ^

   4: 1000004,
   5: 1000005,
   6: 1000006,
   7: 1000007,
   8: 1000008,
-  9: 1000009}
?            ^

+  9: 1000009,
?            ^

+  10: 1000010,
+  11: 1000011,
+  12: 1000012}

When I do the same in a Django unit test, sometimes it prints a diff and sometimes only the first shortened line. I wonder, how can I make it always print the diff.
I run Django tests with ./manage.py test -v 3.

like image 944
planetp Avatar asked May 12 '16 08:05

planetp


People also ask

How to write unit tests for Django?

To write a test you derive from any of the Django (or unittest) test base classes (SimpleTestCase, TransactionTestCase, TestCase, LiveServerTestCase) and then write separate methods to check that specific functionality works as expected (tests use "assert" methods to test that expressions result in True or False values ...

What is MaxDiff Python?

MaxDiff.py This simple python script uses the utility scores from a MaxDiff estimation, and computes the relative scores for each item. It allows for the use of filters and weights in the data.

What is self client in Django?

self. client , is the built-in Django test client. This isn't a real browser, and doesn't even make real requests. It just constructs a Django HttpRequest object and passes it through the request/response process - middleware, URL resolver, view, template - and returns whatever Django produces.


1 Answers

If you set maxDiff to None, then the full diff will be shown.

class A(unittest.TestCase):
    maxDiff = None
    ...
like image 92
Alasdair Avatar answered Oct 12 '22 23:10

Alasdair