Press "Enter" to skip to content

Month: February 2017

使用pytest进行批量测试

Pytest是python的一个测试模块,可以编写一些简单的测试用例,也可以用来进行一些批量测试,下面简单介绍一下。

1,安装

$ pip install -U pytest
or
$ easy_install -U pytest

$ py.test --version

2,基础语法
[code lang=”python”]
#!/usr/bin/python
import pytest

def func(x):
return x + 1

def test_answer(): #测试用例,方法需要以test_开头
assert func(3) == 5

运行之

$ py.test 2.py    #注意需要用py.test命令来运行之

稍微复杂一点儿的写法:
[code lang=”python”]
#!/usr/bin/python
import pytest

class TestClass:

def test_one(self):
x = "this"
assert "h" in x

def test_two(self):
x = 3
assert x > 2

Leave a Comment