0
: 나는단위 테스트를 위해 입력 dir을 조롱하는 방법? 광산의 이전 질문에 관련
import threedi_utils
@mock.patch('threedi_utils.files.os.walk')
def test_get_dir_size_can_get_dir_size(self, mock_walk):
mock_walk.return_value(5000)
size = threedi_utils.files.get_dir_size(self.test_path)
self.assertEqual(size, 5000)
그러나 mock_walk.return_value(5000)
없이가는 os.walk
기능을 조롱해야 이해 것과 그래서
def get_dir_size(dir_path):
"""Determine the size of a dir.
This function also takes into account the allocated size of
directories (4096 bytes).
Note: The function may crash on symlinks with something like:
OSError: [Errno 40] Too many levels of symbolic links
:param dir_path (str): path to the directory
:return: size in bytes.
"""
tot_size = 0
for (root_path, dirnames, filenames) in os.walk(dir_path):
for f in filenames:
fpath = os.path.join(root_path, f)
tot_size += os.path.getsize(fpath)
tot_size += os.path.getsize(root_path)
return tot_size
: How to unit test a method that calculates the size of a dir? 내가 단위 테스트에이 기능을 원하는 내 검사 실패로 인한 영향
Traceback (most recent call last):
File "/home/vagrant/.buildout/eggs/mock-1.3.0-py2.7.egg/mock/mock.py", line 1305, in patched
return func(*args, **keywargs)
File "/srv/lib/threedi_utils/tests/test_files.py", line 55, in test_get_dir_size_can_get_dir_size
self.assertEqual(size, 5000)
AssertionError: 0 != 5000
나는 무엇입니까? ssing?
이는 전혀 의미가 없습니다. 'get_dir_size' 전체를 조롱하면, ** 정확히 당신이 테스트하고있는 것은 무엇입니까?! ** 여기 테스트 한 것은 당신의 모의가 방금 말한 값을 반환한다는 것입니다. 'os.walk'을 조롱하고 싶다면, 그 함수가 실제로 무엇을 리턴하는지 모의이 필요하다. 그래서 함수가 그 함수로 올바른 일을하는지 알 수있다. – jonrsharpe
하하, 알 겠어! 당신은 올바른 방향으로 나를 인도 할 수있는 대답으로 당신의 코멘트를 변형시키고 싶습니까? – LarsVegas
대신, 나는 [docs를 읽으십시오] (https://docs.python.org/3/library/os.html#os.walk) (또는 단지 당신이 조롱하고있는 기능을 실행 *)하고 'return_value'를 작성하십시오. 실제로 반환 할 것으로 예상되는 것을 나타냅니다. 그것이 바로 조롱의 근거입니다. ['unittest.mock'] (https://docs.python.org/3/library/unittest.mock.html) 문서에도 많은 예제가 있습니다. – jonrsharpe