Pytest--详解02

固件和allure报告

一.Fixture固件

  作用:某一些用例、类和模块前后需要执行某一些的操作,以及数据使用数据驱动。

       Fixture完整的方法如下:

@pytest.fixture(scope="作用域",params="数据驱动",autouse="是否自动执 行",ids=“参数别名”,name="Fixture别名")   

scope:可选值: function(函数,默认),class(类),module(模块),package/session(会 话)

  1.基础应用:scope是function,如下在参数中使用execute直接调用

import pytest
#获取数据,数据驱动
def getParms():
    return ['yixin','Titen','tingting']

@pytest.fixture(scope='function',autouse='True',params=getParms())
def execute(request):
    print("固件之前")
    yield request.param
    print("固件之后")

class Test_weixin_api():

    def test_03_api(self,execute):
        print(f'这是{execute}的第三个个用例')

    def test_04_api(self):
        print('这是第四个用例')

  注:1.yiled 和 return区别:return:返回函数的结果,return之后的代码不会执行;yield:带有yield函数叫生成器。yield之后的代码会执行。

    2.params传参的时候,会把每一次读取到的值传给request.param,上面的 request参数和request.param取值方式是固定的。

 

  2.scope为class

  注:1. 通过装饰器@pytest.mark.usefixtures("execute_sql")针对指定的类调用

    2.不装饰,使用autouse='True',就会在每一个类之前之后都调佣。

import pytest

@pytest.fixture(scope="class")
def execute_sql():
print("执行SQL语句")
yield "张三"
print("关闭数据库连接")

@pytest.mark.usefixtures("execute_sql")
class TestApi:

def test_01_Titen(self):
print("titen1")

def test_02_dalao(self):
print("dalao2")

class TestLen:
def test_len(self):
print("testlen")

  3.scope作用域是module或package/session,那么需要结合conftest.py使 用。

  (1)conftest.py专门用于存放固件fixtue的配置文件,名称是固定的,不能更改。

  (2)在conftest.py文件中的fixtue在调用时都不需要导包。

  (3)conftest.py文件可以有多个,并且多个conftest.py文件的多个fixture之间没有冲突。

  (4)模块级别和session模块一般都是自动执行。

 

  4.ids不能单独使用,必须和params一起使用,作用是给参数起别名

 

  5.name作用是给fixture起别名。 特别注意:当name起别名后,那么固件原来的名称就失效了。

 

总结:

pytest执行顺序总结:

  1.查询当前目录下的conftest.py

  2.查询当前目录下的pytest.ini文件,找到测试用例

  3.查询用例目录下的conftest.py文件。

  4.查询用例中是否有setup,teardown,setup_class,teardown_class

  5.执行测试用例。

 

二.基础路径配置

  直接像调用funtion级别的固件一样调用base_url

pytest.ini配置 和 用例调用代码:

[pytest]
addopts = -vs --alluredir=./temp --clean-alluredir
testpaths = ./testcases/test_api.py
python_files = test_*.py
python_classes = Test*
python_functions = test_*
base_url = http://www.baidu.com
markers =
    smoke:冒烟
    user_manage:管理
import pytest
class Test_api():
    def test_01_api(self,base_url):
        print(f'这是第一个用例地址是{base_url}')

三.断言

  使用python自带的断言

四.pytest结合allure-pytest生成allure测试报告(安装allure的前提:cmd中输入java和javac都可以运行,参照jmeter)

  1.安装allure-pytest插件。

  2.官网下载allure包,解压allure包到非中文路径,并且还要设置allue的环境变 量到path中。

D:\Titen\allure\allure-2.13.7\bin;

  验证:需要在dos和pycharm里面都使用如下命令验证: 1 allure ‐‐version 如果pycharm没有验证成功,那么重启pycharm再试。

  3.

  (1)生成Json报告:--alluredir=./temps生成报告 --clean-alluredir 清除报告(1.提前建好temps目录  2.=前后千万不要空格,亲测空格执行错误)
[pytest]
addopts = -vs --alluredir=./temp --clean-alluredir
testpaths = ./testcases/test_api.py
python_files = test_*.py
python_classes = Test*
python_functions = test_*
base_url = http://www.baidu.com
markers =
    smoke:冒烟
    user_manage:管理

  (2)生成allure报告

  注:allure generate 构建allure报告   

    ./temps 根据临时json报告构建     

    -o 输出,output ./reports/reports" 报告路径   

    --clean 清除allure报告

if __name__ == '__main__':
    pytest.main()
    os.system('allure generate ./temp -o ./reports/reports --clean')

 

  每篇一句:

  0:12  一点困意没有呢

posted on 2021-11-29 00:13  Titen  阅读(121)  评论(0编辑  收藏  举报

导航