You should patch the curdir
variable for hello.py
module. So the patched target should be hello.curdir
. For more info, see where-to-patch.
E.g.
hello.py
:
from os.path import curdir
class Hello:
def hey(self):
return curdir
test_hello.py
:
import unittest
from unittest import TestCase
from unittest.mock import patch
from hello import Hello
class TestHello(TestCase):
@patch('hello.curdir', new='mock')
def test_hey(self):
h = Hello()
res = h.hey()
print(res)
assert res == 'mock'
if __name__ == '__main__':
unittest.main()
unit test result:
⚡ coverage run /Users/dulin/workspace/github.com/mrdulin/python-codelab/src/stackoverflow/66861243/test_hello.py && coverage report -m --include='./src/**'
mock
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Name Stmts Miss Cover Missing
------------------------------------------------------------------------
src/stackoverflow/66861243/hello.py 4 0 100%
src/stackoverflow/66861243/test_hello.py 13 0 100%
------------------------------------------------------------------------
TOTAL 17 0 100%