pytest使用方法
1 2 3 4 5 6 7 8
|
py.test -v test_functions.py
py.test -v .
|
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| ''' # 目录结构 test . ├── __init__.py ├── functions.py # 方法文件 ├── test_app.py # 测试文件 └── test_functions.py # 测试文件 '''
def multiple(x,y ): return x*y +10
def add_and_multiply(x,y ): addition=x+y multi=multiple(x,y)
return (addition, multi)
def test_add_and_multiply(): x,y=3,5
addi, multi=add_and_multiply(x,y )
assert addi==8 assert multi==15
@mock.patch('test.functions.multiple', return_value=15 ) def test_add_and_multiply_mock(mock_func): x = 3 y = 5 addi, multi= add_and_multiply(x, y) print() assert addi == 8 assert multi == 15
def test_add_and_multiply_mock2(): x = 3 y = 5 test.functions.multiple=mock.Mock(return_value=15) addi, multi= add_and_multiply(x, y) print() assert addi == 8 assert multi == 15
|
测试
'''bash
指定测试文件
py.test -v test_functions.py
指定测试目录
py.test -v .
py.test -v test_functions.py
collected 3 items
test_functions.py::test_add_and_multiply FAILED [ 33%]
test_functions.py::test_add_and_multiply_mock PASSED [ 66%]
test_functions.py::test_add_and_multiply_mock2 PASSED [100%]
=============== FAILURES ============
___________test_add_and_multiply _____
def test_add_and_multiply():
x,y=3,5
addi, multi=add_and_multiply(x,y )
assert addi==8
assert multi==15
E assert 25 == 15
E -25
E +15
test_functions.py:21: AssertionError
=============== 1 failed, 2 passed in 0.14s ===========