pytest--python的一种测试框架--fixture/YAML/parametrize

一、pytest的fixture详解

fixture概念fixture是pytest用于将测试前后进行预备、清理工作的代码处理机制。

fixture相对于setup和steardown来说有以下几个优势:

1.fixure命名更加灵活,局限性比较小。
2.conftest.py配置里面可以实现数据共享,不需要import就能自动找到一些配置。

fixture是在setup/teardown基础上进行一些优化,我们之前去做一些方法级模块级,比如一个方法中可能定义了好几个测试用例而此时调用setup/steardown,这些测试用例都会执行前置和后置,这时候我们有个需求就是一部分测试用例我不想用前置和后置另外一部分不想这个时候该怎么办这个时候不好去实现,因此fixture就出现了。
fixture需要定义一个方法@pytest.fixture。

老样子,四个级别:

(scop=“function”)每一个函数或方法都会调用;
(scop=“class”)每个类调用一次;
(scop=“module”)每个.py文件调用一次;
(scop=“session”)多个文件调用一次,.py文件就是module.
fixture的作用范围:session>module>class>fuction

(scop=“function”):

函数后面括号里有func的都会执行fixture,这里可以看到它是比较灵活的,你想用就放进去不想用就不放就行。@pytest.fixture(autouse=True)如果括号里面值是True证明是默认的下面每条函数都会执行预备清理工作。

import requests
import pytest

@pytest.fixture()#等价@pytest.fixture(scope="function")
def func():
    print("我是前置步骤~~~")

def test_mobile1(func):
    expect=1
    actual=1
    assert expect==actual#测试专用语句:assert,识别期望与实际值是否相等
def test_mobile2(func):
    expect=1
    actual=1
    assert expect==actual#测试专用语句:assert,识别期望与实际值是否相等
def test_mobile3(func):
    expect=1
    actual=1
    assert expect==actual#测试专用语句:assert,识别期望与实际值是否相等
def test_mobile4():
    expect=1
    actual=1
    assert expect==actual#测试专用语句:assert,识别期望与实际值是否相等

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

在这里插入图片描述

import requests
import pytest

@pytest.fixture(autouse=True)#等价@pytest.fixture(scope="function",autouse=True)
def func():
    print("我是前置步骤~~~")

def test_mobile1():
    expect=1
    actual=1
    assert expect==actual#测试专用语句:assert,识别期望与实际值是否相等
def test_mobile2():
    expect=1
    actual=1
    assert expect==actual#测试专用语句:assert,识别期望与实际值是否相等
def test_mobile3():
    expect=1
    actual=1
    assert expect==actual#测试专用语句:assert,识别期望与实际值是否相等
def test_mobile4():
    expect=1
    actual=1
    assert expect==actual#测试专用语句:assert,识别期望与实际值是否相等

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

在这里插入图片描述

二、YAML语法格式

YAML格式是做数据的,YAML写法:对象:

key:
    child-key1:value
    child-key2:value2

等于:{“key":{“child-key”:“value”,“child-key2”:“value2”}}还有就是数组的形式

hero:
  #字典数据格式
  name: xixizi
  word: 芒果是我最喜欢的水果
  Hp: 445.5
#等同语句:hero:{name: xixizi, word: 芒果是我最喜欢的水果, Hp: 445.5}

heros_name:
  - #数组格式
  - xixi
  - 芒果
  - 草莓
heros:
  - #数组与字典组合格式
  - name: xixi
    word: 芒果
    Hp: 440
heros_name_list:
  - #组合数组嵌套格式
  - - xixi
      - 芒果
      - 草莓

接下来新建一个python代码执行的文件:utils

import yaml
f=open("../config/data.yaml",encoding="utf-8")#先用相对路径,../是去上一层目录的意思,./是当前目录
data=yaml.safe_load(f)#读取
print(data)
print(data['hero'])
print(data['heros_name'])
print(data['heros'])
print(data['heros_name_list'])

比json存储数据更灵活
在这里插入图片描述

三、参数化parametrize

参数化可以组装测试数据,在测试前定义好测试数据,并在测试用例中使用。通过参数化可以提高我们的测试效率,也能一定程度上反映出一个测试化人员的水平。

#单词循环
@pytest.mark.parametrize("a",["b"])
def test_parametrize(a):
    print(a)
#多次循环
@pytest.mark.parametrize("a,b",[("c","d"),("e","f")])
def test_parametrize(a,b):
    print(a,b)
def test_parametrize():
    print("测试parametrize")

在这里插入图片描述

单参数单次循环

import pytest

#单参数单次循环
@pytest.mark.parametrize("key",["value"])

def test_parametrize(key):#括号一定要跟@pytest.mark.parametrize("key",["value"])括号里第一项相等
    print("我是"+key)

在这里插入图片描述

单参数多次循环

import pytest

#单参数多次循环,运行时,将数组里的值分别复制给变量,每赋值一次,运行一次。
@pytest.mark.parametrize("name",["xixi","niannian","xiotaiyang"])
def test_parametrize(name):#括号一定要跟@pytest.mark.parametrize("key",["value"])括号里第一项相等
    print("我是"+name)

在这里插入图片描述
这里我们一般会加个判定:

assert name=="xixi"

在这里插入图片描述
在这里插入图片描述

多参数多循环

import pytest

@pytest.mark.parametrize("name,word",[["xixi","天气很好"],["niannian","太阳很暖"],["xiaotaiyang","风很温柔"]])
def test_parametrize_02(name,word):
    print(f'{name}说:{word}')
import pytest
#不管元组还是数组结果是一样的
@pytest.mark.parametrize("name,word",[("xixi","天气很好"),("niannian","太阳很暖"),("xiaotaiyang","风很温柔")])
def test_parametrize_02(name,word):
    print(f'{name}说:{word}')

在这里插入图片描述

四、注意,经常出错的地方

@pytest.mark.parametrize("name,word",["xixi","天气很好"])
def test_parametrize_02(name,word):
    print(f'{name}说:{word}')

这样看起来好像是正确的但是这个程序运行的时候会报错。也就是说你要是有两个值想给它都赋值,必须用数组包起来。

正确写法:

@pytest.mark.parametrize("name,word",[["xixi","天气很好"]])
  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淅淅同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值