更加优雅轻便的python单元测试模块——pytest【入门篇】

python学习网 2020-10-11 15:36:01

官网介绍 全英警告!!!

https://docs.pytest.org/en/latest/getting-started.html#install-pytest

 

 安装pytest

pip3 install pytest

 

写一个简单的例子

# content of test_sample.py
def func(x):
    return x + 1


def test_answer():
    assert func(3) == 5

 

使用pytest.raises来测试是否发生了意料之中的异常

# content of test_sysexit.py
import pytest


def f():
    raise SystemExit(1)


def test_mytest():
    with pytest.raises(SystemExit):
        f()

 

Note:

使用 '-q' / '--quiet' 来使得输出更加简单

$ pytest -q test_sysexit.py
.                                                                    [100%]
1 passed in 0.12s

 

阅读(2781) 评论(0)