1、pytest认识

(1)pytest比unnitest的优点

(2)pytest使用规则:

测试文件以test_开头(以_test结尾也可以)测试类以Test开头,并且不能带有__init__方法测试函数以test_开头

(3)安装3.8.0版本:

>pip install pytest==3.8.0

(4)pytest一个简单的例子

#test_pyexample.py脚本

import time

#import pytest 不用导入,安装好以后可以直接用

def add(x,y):

return x+y

def test_add():

assert add(1,2)==3

def test_add2():

print("I am 2")

time.sleep(3)

assert add(1.2,1.3)==5.3

assert add(2,2)==4

pycharm控制台运行结果:

Launching pytest with arguments test_pyexample.py::test_add2 in E:\navy_lu\python\exercise_script\pytest_script

============================= test session starts =============================

platform win32 -- Python 3.7.1, pytest-3.8.0, py-1.8.0, pluggy-0.12.0

rootdir: E:\navy_lu\python\exercise_script\pytest_script, inifile:

plugins: sugar-0.9.2collected 1 item

test_pyexample.py FI am 2

test_pyexample.py:8 (test_add2)

5.3 != 2.5

Expected :2.5

Actual :5.3

def test_add2():

print("I am 2")

time.sleep(3)

> assert add(1.2,1.3)==5.3

E assert 2.5 == 5.3

E + where 2.5 = add(1.2, 1.3)

test_pyexample.py:12: AssertionError

[100%]

================================== FAILURES ===================================

__________________________________ test_add2 __________________________________

def test_add2():

print("I am 2")

time.sleep(3)

> assert add(1.2,1.3)==5.3

E assert 2.5 == 5.3

E + where 2.5 = add(1.2, 1.3)

test_pyexample.py:12: AssertionError

---------------------------- Captured stdout call -----------------------------

I am 2

========================== 1 failed in 3.16 seconds ===========================

Process finished with exit code 0

命令行运行结果:

(1)cd 到代码所在的目录,执行命令:py.test test_pyexample.py

(2)安装pytest-sugar插件可以看到进度条

测试结果如下:

2、pytest参数化:@pytest.mark.parametrize()

(1)单个参数

使用装饰器:@pytest.mark.parametrize()

import pytest

import random

@pytest.mark.parametrize('x',[(1),(2),(6)])

def test_add(x):

print(x)

assert x==random.randrange(1,7)

运行结果:

 

(2)多个参数:

import pytest

@pytest.mark.parametrize('x,y',[

(1+2,3),

(2-0,1),

(6*2,12),

(10*2,3),

("test","test"),

])

def test_add(x,y): #必须与上面保持一致,只能用x,y不能用其他字母

assert x==y

运行结果:

 

3、pytest多个assert:pytest-assume

安装插件:pip install pytest-assume

使用多个assert,结果中只有第一个错误,没有报出第二个

import pytest

def test_multiple_assert():

assert (1==2)

assert (2==2)

assert (3==0)

执行结果:

 使用assume ,会把所有错误都报出来:

4、重新运行失败的用例:pytest- rerunfailures

安装插件:pip install pytest- rerunfailures

(pytest的安装路径)>pytest --reruns n 脚本路径,

(pytest的安装路径)>pytest -s --reruns n 脚本路径,

import random

def add(x,y):

return x+y

def test_add():

random_value=random.randint(2,7)

print('random_value:'+str(random_value))

assert add(1,3)==random_value

运行命令:

C:\Users\ldld0\AppData\Local\Programs\Python\Python37\Lib>

pytest --rerunsE:\navy_lu\python\exercise_script\pytest_script\test_pyexample_rerun.py

第一次运行:(整个过程:一共运行4次,第一次运行失败,后面重新运行3次都失败了)

 

第二次运行:(整个过程:一共运行了4次,第一次失败了,三个R,表示重新运行3次,第三次重新运行成功;如果重新运行第1次就成功了,就算设置的是重新运行3次,后面两次也不会再运行)

5、控制测试运行顺序:pytest--ordering

安装插件pip install pytest-ordering

借助于装饰器@pytest.mark.run(order=1)控制测试运行的顺序

import time

value=0

def test_add2():

print("I am 2")

time.sleep(2)

assert value==10

def test_add():

print("I am add")

global value

value=10

执行结果:

使用装饰器:

import pytest

import time

value=0

@pytest.mark.run(order=2) #后执行order=2

def test_add2():

print("I am 2")

time.sleep(2)

assert value==10

@pytest.mark.run(order=1) #先执行order=1

def test_add():

print("I am add")

global value

value=10

assert value==10

运行结果:

​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。

如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受

可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛

分享他们的经验,还会分享很多直播讲座和技术沙龙

可以免费学习!划重点!开源的!!!

qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

好文推荐

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: 


大家都在找:

python:python怎么读

pytest:pytest是什么

软件测试:软件测试基础知识

程序员:程序员那么可爱电视剧免费观看完整版

接口测试:postman接口测试教程菜鸟教程

自动化测试:自动化测试工具selenium的使用

测试工程师:测试工程师英文

大家都在看: