我有这个功能:
def myMethod(x):
return (x + 1) / x
我需要为此创建3个单元测试,但是我知道每个功能只能创建1个单元测试。 如何为一个功能创建3个单元测试?
答案 0 :(得分:1)
在test_myMethod中,您可以添加任意多个案例 您可以参考https://docs.python.org/3/library/unittest.html了解更多详情
import unittest
class MyCode(unittest.TestCase):
def test_myMethod(self):
self.assertTrue(myMethod(4),1.25)
self.assertFalse(myMethod(4),1)
if __name__ == '__main__':
unittest.main()
答案 1 :(得分:0)
您还可以通过在函数上调用参数来实现此目的,如下所示:
class TestMyFunction:
@pytest.mark.parametrize("input, expected", [
(4, 1.25),
(1, 1)
])
def test_my_function(input, expected):
assert myMethod(input) == expected