pytest 基本用法

1、断言用assert,可以进行==,!=,+,-,*,/,<=,>=,is True、False,is not True、False ,in ,not in 等判断。

 

import pytest
def add(a,b):
return a + b

def is_prime(n):
if n <= 1:
return False
for i in range(2,n):
if n % i == 0:
return False
return True

def test_add_1():
'''测试相等'''
assert add(3,4) == 7

def test_add_2():
'''测试不相等'''
assert add(12,3) != 16

def test_add_3():
'''测试大于或等于'''
assert add(17,22) <= 50

def test_add_4():
'''测试小于或等于'''
assert add(17,22) >=38

def test_in():
'''测试包含'''
a = 'hello'
b = 'he'
assert b in a

def test_not_in():
'''测试不包含'''
a = 'hello'
b = 'hi'
assert b not in a

def test_true_1():
'''判断是否为True'''
assert is_prime(13) is True

def test_true_2():
'''判断是否为True'''
assert is_prime(7) is True

def test_true_3():
'''判断是否不为True'''
assert is_prime(4) is not True


def test_true_4():
'''判断是否不为True'''
assert is_prime(6) is not True

def test_false_1():
'''判断是否为False'''
assert is_prime(8) is False

if __name__ == '__main__':
pytest.main()

2、测试文件和测试函数必须以“test”开头,测试类必须以‘Test’开头。

3、可以通过main()方法执行测试用例。需要指定参数和路径,还可以指定某个测试类或测试方法用“::”隔开。如:

 

pytest.main(['-s','./test_fixtures_01.py::test_multiply_5_6'])

 

4、Pytest提供了丰富的参数运行测试用例,‘-s’:关闭捕捉,输出打印信息。‘-v’:用于增加测试用例的冗长。‘-k’ :运行包含某个字符串的测试用例。如:pytest -k add XX.py 表示运行XX.py中包含add的测试用例。‘q’:减少测试的运行冗长。‘-x’:出现一条测试用例失败就退出测试。在调试阶段非常有用,当测试用例失败时,应该先调试通过,而不是继续执行测试用例。pytest还可以运行测试目录下的所有测试用例:pytest 目录路径

5、Fixtrue

 

import pytest

#功能函数
def multiply(a,b):
return a * b

class TestMultiply:
#=======fixtures========
@classmethod
def setup_class(cls):
print('setup_class==============================>')

@classmethod
def teardown_class(cls):
print('teardown_class==========================>')

def setup_method(self,method):
print('setup_method============================>')

def teardown_method(self,method):
print('teardown_method============================>')

def setup(self):
print('setup======================================>')

def teardown(self):
print('teardown===================================>')

 

6、参数化。pytest本身是支持参数化的,不需要安装插件。

 

import pytest
import math

#pytest参数化
@pytest.mark.parametrize('base,exponent,expected',[(2,2,4),(2,3,8),(2,4,16),(0,9,0)],ids = ['case1','case2','case3','case4'])
def test_pow(base,exponent,expected):
assert math.pow(base,exponent) == expected

if __name__ == '__main__':
pytest.main(['-s','./test_parameterized.py'])

 

7、conftest.py 是pytest的本地测试配置文件。可以设置项目级别的Fixtrue还可以导入外部插件,还可以指定钩子函数。conftest.py值作用于它所在的目录及子目录。

import pytest

#设置钩子函数

@pytest.fixtrue()

def test_url():

  return 'http://www.baidu.com'

8、pytest-html 插件可以生成HTML格式的测试报告。支持测试用例失败 的截图。对于web自动化测试来说非常有用。

运行:pytest 用例路径 --html=./report/result.html            注意:--html= 没有空格。

还可以用main()方法来运行:pytest.main(['当前用例路径','--html=测试报告/XX.html '])

9、pytest-rerunfailures 插件可以在测试用例失败时进行重试。通过‘--reruns 重试次数’来设置测试用例运行失败后的重试次数。

pytest 用例路径 --reruns 3   

相对于unittest框架,pytest更适合做UI自动化:1、pytest通过conftest.py文件配置全局浏览器的启动或关闭,整个自动化项目只需要启动或者关闭一次浏览器即可,将节省用例运行时间和开销。2、pytest支持用例运行失败截图。通过pytest-html可以实现,需要在conftest.py配置即可。3、测试用例失败后重跑。UI自动化测试的稳定性是个问题。有很多不可控的因素会导致测试用例的失败,pytest-rerunfailurea可以实现用例重跑。


 

posted @ 2019-10-02 21:19  林深时见鹿  阅读(1801)  评论(0编辑  收藏  举报