pytest自定义程序运行顺序

 

# 自定义用例的执行顺序:fixture
# conftest.py配置数据共享,不需要导入新包就能自动找到一些配置
# scope="module"(可以实现多个.py跨文件共享前置)
# scope="session"(以实现多个.py跨文件使用一个session来完成多个用例)
# scope="function"(默认值)
# scope="class"

'''
fixture(scope="function",params=None,autouse=False,ids=None,name=None)
params  参数列表
antouse
ids 字符串的列表
name 装饰函数的名称
'''
函数级别
import pytest


def test_s1(login):
    print("用例1:登录之后其它动作111")


# 不传login
def test_s2():
    print("用例2:不需要登录,操作222")


def test_s3(login):
    print("用例3:登录之后其它动作333")


if __name__ == "__main__":
    pytest.main(["-s","test_selfOrder.py"])

可以写在配置文件中,供多个用例执行,并不需要重新导入

1、名字固定未conftest.py

2、与要执行的测试用例在同一目录下

3、要有__init__文件

4、不需要导入

pytest自定义程序运行顺序_第1张图片

以上是函数式级别的,针对函数有效,每个函数调用是执行一次

pytest自定义程序运行顺序_第2张图片

模块级别

整个.py文件都会生效,用例调用时,写上函数名称就行了

在整个模块中在被调用前时只执行一次

import pytest


@pytest.fixture(scope="module")
def open():
    print("打开浏览器,并且打开百度首页!")


def test_s1():
    print("用例1:搜索Python1")

def test_s2():
    print("用例2:搜索Python2")

def test_s3(open):
    print("用例3:搜索Python3")


if __name__ == "__main__":
    pytest.main(["-s","test_selfOrderbyModule.py"])

pytest自定义程序运行顺序_第3张图片

yield执行teardown

pytest自定义程序运行顺序_第4张图片

pytest自定义程序运行顺序_第5张图片

最后还是执行teardown

设置autouse=True

import time
import pytest


@pytest.fixture(scope="module", autouse=True)
def start(request):
    print('\n----开始执行module----')
    print("module: %s \n----回到首页---" % request.module.__name__)
    print("----启动浏览器----")
    yield
    print("-----结束测试,end-----")


@pytest.fixture(scope="function", autouse=True)
def open_home(request):
    print("function: %s \n----回到首页---" % request.function.__name__)


def test_01():
    print("----用例01-----")


def test_02():
    print("----用例02-----")


def test_03():
    print("----用例03-----")


if __name__ == "__main__":
    pytest.main(["-s", "test_AutoIsTrue.py"])

pytest自定义程序运行顺序_第6张图片

参数化

标记失败的直接跳过

断言和异常

pytest自定义程序运行顺序_第7张图片

常用断言

assert x      判断什么是否为真

assert not x    判断什么是否为假

assert a in b    判断b包含a

assert a==b    判断a等于b

assert a!=b    判断a不等于b

你可能感兴趣的:(UI自动化测试)