Why doesn't unittest.mock.ANY work correctly w

2019-08-28 02:33发布

问题:

I have written a test in Django, and I'm using unittest.mock.ANY to ignore certain values in a dictionary. Here is the test:

from django.test import TestCase
from django.contrib.auth import get_user_model
import unittest.mock as mock

class Example(TestCase):
    def test_example(self):
        user = get_user_model().objects.create_user(username='example')
        result = {'user': user, 'number': 42}
        self.assertEqual(
            result,
            {'user': mock.ANY, 'number': 42}
        )

If I run this test, I expect it to pass. Instead, I get this failure:

======================================================================
FAIL: test_example (example.tests.Example)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example/tests.py", line 18, in test_example
    'number': 42,
AssertionError: {'user': <User: example>, 'number': 42} != {'user': <ANY>, 'number': 42}
- {'number': 42, 'user': <User: example>}
?                         ^^^^^^^^^^^^^

+ {'number': 42, 'user': <ANY>}
?                         ^^^

Why doesn't ANY work in this case? It seems to work with strings and numbers.

回答1:

assertEqual, in the course of comparing its two arguments, evaluates the expression user == mock.ANY. In standard fashion, the left argument determines which function actually implements ==. In this case, you have user.__eq__(mock.ANY). It appears that whatever type user is, its __eq__ method simply returns False for an unexpected type. If it raised NotImplemented instead, the language would fall back on mock.ANY.__eq__(user), which could return True.

If you change the call to

self.assertEqual(
            {'user': mock.ANY, 'number': 42},
            result,
        )

then the resulting comparison mock.ANY == user will return True as expected.