すべてのfun1()、fun2()、fun3()が結果を返したら、私はパラメータ化に行きます。
# 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 =================================================================================
限り出力は正しいです、どの関数が呼び出されたかは本当に気になりますか? – fmarc
@fmarcもちろん、3つの可能な入力をテストして出力をチェックすることができますが、これは3つの関数fun1、fun2、fun3をすべて実行しなければならないことを意味し、それを行うもっとエレガントな方法を探しています。 – Ziva
fun1/2/3のテストを直接書くこともできますか? my_fun()にはほとんどコードが含まれていないため、テストを書くことはほとんどありません。 – fmarc