pytest测试框架下


活动地址:CSDN21天学习挑战赛

学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您:
想系统/深入学习某技术知识点…
一个人摸索学习很难坚持,想组团高效学习…
想写博客但无从下手,急需写作干货注入能量…
热爱写作,愿意让自己成为更好的人…

欢迎参与CSDN学习挑战赛,成为更好的自己,请参考活动中各位优质专栏博主的免费高质量专栏资源(这部分优质资源是活动限时免费开放喔~),按照自身的学习领域和学习进度学习并记录自己的学习过程。您可以从以下3个方面任选其一着手(不强制),或者按照自己的理解发布专栏学习作品,参考如下:

学习日记

pytest-fixture的用法

场景:用例1需要登录,用例2 不需要登录,用例三需要登录

这种场景是setup和teardown无法实现的,但是可以用pytest-fixture进行实现,其用法:在方法面前增加@pytest.fixture()

例子1:前端自动化中应用

场景:部分用例需要登录 部分用例不需要登录,默认是scope(范围)function

其步骤为:1:导入pytest,2:在登录的函数上面加@pytest.fixture(),3:在要使用的测试方法中传入(登录函数名称),就先登录,4:不传入的就不登录直接执行测试方法

import pytest
@pytest.fixture()
def login():
    print("这个是登入方法")

def test_one(login):
    print("开始执行 test_one方法,需要登录")
    x = 'hello'
    assert 'e' in x

def test_two():
    print("开始执行 test_two方法,不需要登录")
    x = 'hello'
    assert 'e' in x

def test_three(login):
    print("开始执行 test_three方法,需要登录")
    a = 'hello'
    b = 'hello world'
    assert a in b

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

代码结果:

pytest测试框架下

前端自动化中引用—conftest

场景:与其他测试工程师合作一起开发是,公共模块要在不同文件中,要在大家都访问到的地方

解决:conftest.py这个文件进行数据共享,并且他可以放在不同位置起着不同的范围共享作用

执行:系统执行到login时先从本文件中查找是否有这个名字的变量,之后在conftest.py中找是否有

步骤:将登录模块带@pytest.fixture()写在conftest.py

代码:

import pytest
@pytest.fixture()
def login():
    print("这是个登录方法")

def pytest_configure(config):
    marker_list = ("search", "login") #标签名集合
    for marker in marker_list
        config.addinivalue_line(
            "marker", marker
        )

注意事项:文件名不可以更换,文件与运行的用例在同一个包下,且汉含有__init__.py 文件

前端自动化中应用-yield

场景:已经将测试方法钱要执行的货依赖的解决了,测试方法后销毁清除数据的要如何执行呢

解决:通过在同一模块中加入yield关键字,yield是调用第一次返回结果,第二次执行条下面的语句返回,步骤:在@pytest.fixture(scope=module)

在登录的方法中加入yield之后加销毁清除的步骤,注意,这种方式没有返回值,如果希望返回使用addfinalizer

import pytest

@pytest.fixture(scope="moudle")
def open():
    print("打开浏览器")
    yield

    print("执行teardown!")
    print("最后关闭浏览器")
def test_search1(open):
    print("test_search1")
    raise NameError
    pass

def test_search2(open):
    print("test_search2")
    pass

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

fixture的自动应用

场景:不想原测试方法有任何改动,或全部都自动实现引用 ,没特例,也都不要返回值是可以选择自动应用

解决:使用fixture中参数autouse=Ture实现

步骤在方法上@pytest.fixture(autouse=True)

在测试方法上加@pytest.mark.usefixtures(start)

import pytest

@pytest.fixture(autouse=True)
def open():
    print("打开浏览器")
    yield

    print("执行teardown!")
    print("最后关闭浏览器")
def test_search1(open):
    print("test_search1")
    raise NameError
    pass

def test_search2(open):
    print("test_search2")
    pass

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

例子5:fixture带参数传递

场景:在测试中为了数据领过,一般都是通过参数传的

解决:fixture通过固定参数request传递

步骤”在fixture中增加”@pytest.fixture(params=[1,2,3,’linda’])在方法参数中写request

import pytest
@pytest.mark.parametrize("test_input,espected",[(3+5, 8), (2+5, 7), (7*5, 30)])
def test_eval(test_input, expected):
    assert eval(test_input) == expected
test_user_data = ('1', '20')
@pytest.fixture(scope='moudle')
def login_r(request):
    user = request.param
    print(f"\n 打开首页准备登录,登录账户:{user}")
    return user

@pytest.mark.parametrize("login_r", test_user_data,indirect=True)
def test_login(login_r):
    a = login_r
    print(f"测试用力中logind的返回值:{a}")
    assert a !=""

make中的skip和xfail

skip使用场景:

调试时不想运行这个用例,标记无法在某些平台上运行的测试功能,在某些版本中执行,在某些版本中跳过,当外部资源不可用时跳过,

解决方法:@pytest.Mark。skip跳过这个测试用例,可以加skipif在满足某些条件下才希望通过,否则跳过这个用例

xfail场景:功能测试尚未试试货尚未修复的错误,当测试通过时尽管预计回事表,它是一个xpass将在测试摘要中报告,希望测试由于某些情况就应该失败

解决:@pytest.mark.xfail

使用自定义标记mark只执行某些测试用例

在测试用例方法上加@pytest.Mark.webtest

执行-s参数,输出所有测试用的print信息,

-m执行自动以标记相关用例:pytest-s test _mark_zi_09.py -m=webtest

多线程并行与分布式执行

用例多,执行时间长

解决:pytest分布式执行插件 pytest_xdist,多个cpu或主机执行,前提:用例之间都是独立的没有先后顺序

安装:pip3 install pytest-xdist

多个cpu并行执行用例,直接加-n3 是并行数量:pytest-n 3

在多个终端下一起执行

pytest-html生成测试报告

安装:pip3 install pytest-html

生成报告:pytest -v -s –html=report.html — self-contained-html

Original: https://blog.csdn.net/m0_60566330/article/details/126254345
Author: Test冀
Title: pytest测试框架下

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/776282/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球