02-pytest使用

11/26/2022 pytest

# 简单使用

# ! /usr/bin/python3
# coding=utf-8
# @Time: 2022/11/21 20:32
# @Author: xueyan pan
import pytest
import sys


def func(x):
    return x + 1


def test_a():
    print("---{}---\n".format(sys._getframe().f_code.co_name))
    assert func(3) == 5


def test_b():
    print("---{}---\n".format(sys._getframe().f_code.co_name))
    assert func(3) == 4


if __name__ == "__main__":
    # pytest.main 参数为列表, -s:控制台标准输出
    pytest.main(["-s", "test_01.py"])
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

# 运行结果如下:

============================= test session starts =============================
collecting ... collected 2 items

test_01.py::test_a FAILED                                                [ 50%]---test_a---


test_01.py:12 (test_a)
4 != 5

Expected :5
Actual   :4
<Click to see difference>

def test_a():
        print("---{}---\n".format(sys._getframe().f_code.co_name))
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_01.py:15: AssertionError

test_01.py::test_b PASSED                                                [100%]---test_b---



========================= 1 failed, 1 passed in 0.25s =========================

Process finished with exit code 1
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
Last Updated: 11/27/2022, 10:06:05 PM