python测试

pytest模块

安装

1
pip3 install pytest

第一个案例

demo.py

1
2
3
4
5
6
7
8
def func(x):
return x + 1

def test_func1():
assert func(4) == 5 # 成功示例

def test_func2():
assert func(3) == 5 # 失败示例

test_create.py

1
2
3
4
5
6
7
class TestClass(object):
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')

执行方法

1
pytest  文件名 # 可以执行文件中的测试代码

unittest框架

htmlreport模块