pytest测试框架学习笔记 (上)


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

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


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

学习日记

pytest介绍

pytest是一个非常成熟的全功能的Python测试框架

​特点:1:支持测试:2:可以进行简单的单元测试,以及复杂的功能测试,还可以使用selenium/appnium等自动化测试

pytest需要安装的pip包:

pip install -u pytest u(升级)

pip install pytest-sugar

pip install pytest-rerunfailures

pip install pytest-xdist

pip install pytest-assume

pip install pytest-html

测试用例的识别与运行

测试文件:test_*.py,*_test.py

用例识别: Test*类包含的所有test_*的方法(测试列不能带有__init__方法),不在class中的所有的test_*方法

pytest也可以执行unittest框架写的用例和方法

使用pycharm运行pytest框架设置

 

pytest代码实操

def test_one():
    print("开始执行 test_one方法")
    x = 'this'
    assert 'h' in x


def test_two():
    print("开始执行 test_two方法")
    x = 'hello'
    assert 'e' in x


def test_three():
    print("开始执行 test_three方法")
    a = 'hello'
    b = 'hello world'
    assert a in b



运行结果:

 运行方法:

import pytest
class TestDemo():
    def test_one():
        print("开始执行 test_one方法")
        x = 'this'
        assert 'h' in x
    
    
    def test_two():
        print("开始执行 test_two方法")
        x = 'hello'
        assert 'e' in x
    
    
    def test_three():
        print("开始执行 test_three方法")
        a = 'hello'
        b = 'hello world'
        assert a in b

if __name__=='__main__':
    pytest.main("-v -x TestDemo")#方法1:直接运行类
    pytest.main(['-v'' -x ''TestDemo'])#方法2,放在列表中进行运行

如果运行所有的数据 在main()括号中可以什么数据都不放

其框架结构:

import pytest 类似的setup,teardown同样更加领过

模块级(setup_module/teardown_module)模块始末,全局的(优先最高)

函数级(setup_function/teardown_function)支队函数用例生效(不在类中)

类级(setup_class/teardown_class)只在类中前后运行一次(在类中)

方法级(setup_methond/teardown_methond)开始于方法始末(在类中)

类里面的(setup/teardown)运行在调用方法的前后

例子:看运行先后顺序

import pytest

def setup_module():
    print("这个是setup_module 方法")

def teardown_module():
    print("这个是 /teardown_module方法")

def setup_function():
    print("这个是setup_function 方法")

def teardown_function():
    print("这个是teardown_function方法")
def test_login():
    print('外部的方法')
class testDemo():
    def setup_calss(self):
        print("setup_class")
    def setup_method(self):
        print("setup_method")

    def setup(self):
        print("setup")

    def teardown_class(self):
        print("teardown_class")

    def teardown_methond(self):
        print("teardown_methond")

    def teardown(self):
        print("teardown")

    def test_one(self):
        print("开始执行 test_one方法")
        x = 'this'
        assert 'h' in x

    def test_two(self):
        print("开始执行 test_two方法")
        x = 'hello'
        assert 'e' in x

    def test_three(self):
        print("开始执行 test_three方法")
        a = 'hello'
        b = 'hello world'
        assert a in b
if __name__=='__main__':
    pytest.main("-v -s")

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值