模块安装 - 模拟

时间:2017-04-07 13:26:47

标签: python

我正在尝试使用以下代码片段来模拟一个类:

import unittest
from mock import Mock

class TestMocking(unittest.TestCase):
   def test_mock_method_returns(self):
       my_mock = Mock()
       my_mock.my_method.return_value = "hello"
       self.assertEquals("hello",my_mock.my_method())


if __name__ == "__main__":
     unittest.main()

运行代码时,出现以下错误:

ImportError: No module named mock

我已使用以下命令安装了mock:     pip install mock

我有以下输出:

Collecting mock
  Downloading mock-2.0.0-py2.py3-none-any.whl (56kB)
  100% |████████████████████████████████| 61kB 1.4MB/s 
Requirement already satisfied: six>=1.9 in 
  /home/sajjad/Dokument/InstalledProgram/anaconda3/lib/python3.5/site-packages (from mock)
  Collecting pbr>=0.11 (from mock)
  Downloading pbr-2.0.0-py2.py3-none-any.whl (98kB)
  100% |████████████████████████████████| 102kB 3.4MB/s 
  Installing collected packages: pbr, mock
  Successfully installed mock-2.0.0 pbr-2.0.0

1 个答案:

答案 0 :(得分:0)

您正在使用Python 3.5;该版本中的mock已经part of the standard library,您无需安装它。你应该像这样导入它:

from unittest.mock import Mock