Python - 单元测试、模拟、补丁、输入
Python - unittest, mock, patch, input
所以我的代码有问题。
文件 1:
class Abc(object):
...
def function1(self):
#do something
def function2(self):
x = input()
return x+1
现在我正在尝试测试函数 2,所以我为它编写了一个测试,但我不知道我做错了什么:
from unittest.mock import patch
import unittest
from file1 import *
class TestBackend(unittest.TestCase):
def test_mode_first(self):
self.assertEqual(Abc().funcion1(), 30)
@patch('funcion2.input', create=True)
def test_mode_second(self, mocked_input):
mocked_input.side_effect = ["QWE"]
result = Abc().funcion2()
self.assertEqual(result, 10)
if __name__ == '__main__':
unittest.main()
我收到 ModuleNotFoundError:没有名为 'function2' 的模块
那么我在这里做错了什么?
感谢您的帮助:)
你得到 ModuleNotFoundError
因为 funcion2
不是一个模块。 patch doc
清楚这一点:
target should be a string in the form 'package.module.ClassName'. The
target is imported and the specified object replaced with the new
object, so the target must be importable from the environment you are
calling patch() from. The target is imported when the decorated
function is executed, not at decoration time.
当从文件所在的目录中使用 python3 -m unittest discover
执行时,这对我有用。
顺便说一句,您的示例中有几个拼写错误,例如Abc().funcion2()
,请注意 funcion2
.
中缺少的 t
另外,尽量不要使用from … import *
:https://docs.quantifiedcode.com/python-anti-patterns/maintainability/from_module_import_all_used.html#using-wildcard-imports-from-import
# file1.py
class Abc(object):
def function1(self):
return 30
def function2(self):
x = input()
return x + "1"
# test_file1.py
import unittest
from unittest.mock import patch
from file1 import Abc
class TestBackend(unittest.TestCase):
def test_mode_first(self):
self.assertEqual(Abc().function1(), 30)
@patch('builtins.input')
def test_mode_second(self, mocked_input):
mocked_input.return_value = "QWE"
result = Abc().function2()
self.assertEqual(result, "QWE1")
所以我的代码有问题。 文件 1:
class Abc(object):
...
def function1(self):
#do something
def function2(self):
x = input()
return x+1
现在我正在尝试测试函数 2,所以我为它编写了一个测试,但我不知道我做错了什么:
from unittest.mock import patch
import unittest
from file1 import *
class TestBackend(unittest.TestCase):
def test_mode_first(self):
self.assertEqual(Abc().funcion1(), 30)
@patch('funcion2.input', create=True)
def test_mode_second(self, mocked_input):
mocked_input.side_effect = ["QWE"]
result = Abc().funcion2()
self.assertEqual(result, 10)
if __name__ == '__main__':
unittest.main()
我收到 ModuleNotFoundError:没有名为 'function2' 的模块 那么我在这里做错了什么?
感谢您的帮助:)
你得到 ModuleNotFoundError
因为 funcion2
不是一个模块。 patch doc
清楚这一点:
target should be a string in the form 'package.module.ClassName'. The target is imported and the specified object replaced with the new object, so the target must be importable from the environment you are calling patch() from. The target is imported when the decorated function is executed, not at decoration time.
当从文件所在的目录中使用 python3 -m unittest discover
执行时,这对我有用。
顺便说一句,您的示例中有几个拼写错误,例如Abc().funcion2()
,请注意 funcion2
.
t
另外,尽量不要使用from … import *
:https://docs.quantifiedcode.com/python-anti-patterns/maintainability/from_module_import_all_used.html#using-wildcard-imports-from-import
# file1.py
class Abc(object):
def function1(self):
return 30
def function2(self):
x = input()
return x + "1"
# test_file1.py
import unittest
from unittest.mock import patch
from file1 import Abc
class TestBackend(unittest.TestCase):
def test_mode_first(self):
self.assertEqual(Abc().function1(), 30)
@patch('builtins.input')
def test_mode_second(self, mocked_input):
mocked_input.return_value = "QWE"
result = Abc().function2()
self.assertEqual(result, "QWE1")