PyTest:测试if语句中调用了哪个函数
PyTest: test which function was called in the if statement
我有一个函数,它考虑三种不同的情况,并且针对每种情况调用不同的函数,如下例所示
def my_fun(input):
if input == 1:
fun1()
if input == 2:
fun2()
if input == 3:
fun3()
我想使用 py.test
为函数 my_fun
编写测试,但我不知道如何测试是否为给定输入调用了正确的函数?
为什么多个 if 语句而不是 elif?他们的条件是互斥的,为什么要浪费另一张支票呢?但无论如何,虚拟打印怎么样,像这样:
def my_fun(input):
if input == 1:
fun1()
print("Function 1 executed ?")
elif input == 2:
fun2()
print("Function 2 executed ?")
elif input == 3:
fun3()
print("Function 3 executed ?")
如果我有足够的声誉,我会顺便评论一下。 #JustWannaGetTo50
正如@fmarc 评论的那样,调用哪个函数不如测试 my_fun
做正确的事情重要。但是,您可以模拟这三个函数中的每一个,然后独立于每个函数实际执行的操作来测试是否调用了正确的函数。 (请注意,mock
是 Python 2 中的第 3 方模块,您需要安装它;它在 Python 3.3(?) 中的标准库中可用,如 unittest.mock
.)
一个简单的例子:
import mock
def test_my_fun():
with mock.patch('fun1', wraps=fun1) as mock_fun1:
with mock.patch('fun2', wraps=fun2) as mock_fun2:
with mock.patch('fun3', wraps=fun3) as mock_fun3:
my_fun(1)
mock_fun1.assert_called_with()
mock_fun2.assert_not_called()
mock_fun3.assert_not_called()
查看 mock
安装文档,了解支持哪些方法。
如果所有 fun1()、fun2() 和 fun3() return 结果我会进行参数化。
# Here an example of functions to be tested
def fun1():
return 1
def fun2():
return 2
def fun3():
return 3
def my_fun(input):
if input == 1:
return fun1()
if input == 2:
return fun2()
if input == 3:
return fun3()
这里是测试矩阵。预期结果也是对被测函数的调用,因此您可以匹配测试中的函数调用(如您在问题中所问):
# Parametrized test
import pytest
@pytest.mark.parametrize("test_input, expected", [(1, fun1()), (2, fun2()), (3, fun3())])
# ---------------------------------------
# test_input | 1 | 2 | 3 |
# ---------------------------------------
# expected | fun1() | fun2() | fun3() |
# ---------------------------------------
def test_my_fun(test_input, expected):
assert my_fun(test_input) == expected
测试运行:
mac: py.test -v test_parametrization.py
=================================================================================== test session starts ====================================================================================
platform darwin -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
cachedir: .cache
rootdir: /Users/user/Repos/playground, inifile:
plugins: hidecaptured-0.1.2, instafail-0.3.0
collected 3 items
test_parametrization.py::test_my_fun[1-1] PASSED
test_parametrization.py::test_my_fun[2-2] PASSED
test_parametrization.py::test_my_fun[3-3] PASSED
================================================================================= 3 passed in 0.01 seconds =================================================================================
我有一个函数,它考虑三种不同的情况,并且针对每种情况调用不同的函数,如下例所示
def my_fun(input):
if input == 1:
fun1()
if input == 2:
fun2()
if input == 3:
fun3()
我想使用 py.test
为函数 my_fun
编写测试,但我不知道如何测试是否为给定输入调用了正确的函数?
为什么多个 if 语句而不是 elif?他们的条件是互斥的,为什么要浪费另一张支票呢?但无论如何,虚拟打印怎么样,像这样:
def my_fun(input):
if input == 1:
fun1()
print("Function 1 executed ?")
elif input == 2:
fun2()
print("Function 2 executed ?")
elif input == 3:
fun3()
print("Function 3 executed ?")
如果我有足够的声誉,我会顺便评论一下。 #JustWannaGetTo50
正如@fmarc 评论的那样,调用哪个函数不如测试 my_fun
做正确的事情重要。但是,您可以模拟这三个函数中的每一个,然后独立于每个函数实际执行的操作来测试是否调用了正确的函数。 (请注意,mock
是 Python 2 中的第 3 方模块,您需要安装它;它在 Python 3.3(?) 中的标准库中可用,如 unittest.mock
.)
一个简单的例子:
import mock
def test_my_fun():
with mock.patch('fun1', wraps=fun1) as mock_fun1:
with mock.patch('fun2', wraps=fun2) as mock_fun2:
with mock.patch('fun3', wraps=fun3) as mock_fun3:
my_fun(1)
mock_fun1.assert_called_with()
mock_fun2.assert_not_called()
mock_fun3.assert_not_called()
查看 mock
安装文档,了解支持哪些方法。
如果所有 fun1()、fun2() 和 fun3() return 结果我会进行参数化。
# Here an example of functions to be tested
def fun1():
return 1
def fun2():
return 2
def fun3():
return 3
def my_fun(input):
if input == 1:
return fun1()
if input == 2:
return fun2()
if input == 3:
return fun3()
这里是测试矩阵。预期结果也是对被测函数的调用,因此您可以匹配测试中的函数调用(如您在问题中所问):
# Parametrized test
import pytest
@pytest.mark.parametrize("test_input, expected", [(1, fun1()), (2, fun2()), (3, fun3())])
# ---------------------------------------
# test_input | 1 | 2 | 3 |
# ---------------------------------------
# expected | fun1() | fun2() | fun3() |
# ---------------------------------------
def test_my_fun(test_input, expected):
assert my_fun(test_input) == expected
测试运行:
mac: py.test -v test_parametrization.py
=================================================================================== test session starts ====================================================================================
platform darwin -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
cachedir: .cache
rootdir: /Users/user/Repos/playground, inifile:
plugins: hidecaptured-0.1.2, instafail-0.3.0
collected 3 items
test_parametrization.py::test_my_fun[1-1] PASSED
test_parametrization.py::test_my_fun[2-2] PASSED
test_parametrization.py::test_my_fun[3-3] PASSED
================================================================================= 3 passed in 0.01 seconds =================================================================================