Python unittest when to test for value or object equality

146 Views Asked by At

I have the following code that I'm testing.

main.py

import helpers

def do_something(some_arg):
    ...
    return helpers.help_do_something(some_arg)

test_main.py

import unittest
from unittest import mock

class TestDoSomething(unittest.Testcase):
    @mock.patch('path.to.patch')
    def setUp(self, *_):
        import main
        self.main = main

    @mock.patch('main.helpers')
    def test_0_1(self, helpers_mock):
        ret = self.main.do_something('test_arg')
        self.assertIs(ret, helpers_mock.help_do_something.return_value)

When I'm testing the return value of do_something my instinct is telling me that the comparison should be asserting object equality, not value equality. Am I correct in thinking this? I'm having a difficult time articulating why this should be the case.

More generally, when should we be testing for object equality versus value equality in unit testing?

1

There are 1 best solutions below

0
On

It's unlikely that you want to use object equality here. The only case would be, if your mock function would return the same object (not an equally valued object) as the function you are testing, which could only happen if they either take it from input parameters (which is not the case here) or from some common pool of objects.