Pytest环境部署+用例执行管理+用例参数化

38 篇文章 6 订阅
23 篇文章 0 订阅


1.命名规范

import pytest


# 测试函数
def testCase():
    print("discover目录下的测试用例testCase")


if __name__ == '__main__':
    pytest.main(['-s'])

# 测试函数
def test02():
    print("doc目录下的用例test02")

# 测试类以Test
class TestDemo:
    # 不能在pytest类中加构造方法
    # def __init__(self):
    #     print("构造方法")


    # 测试方法
    def test_case_03(self):
        print("类下的用例")

import pytest

def test_01():
    print("hello")
    assert 1 == 1

if __name__ == '__main__':
    pytest.main(['-s'])

import unittest


class UnitDemo(unittest.TestCase):
    def test_login(self):
        print("login")
        self.assertEqual("123","1231",msg = "断言失败")

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

2.断言+rerun+setup

pytest.ini
[pytest]
markers =
    smoke: marks tests as smoke
    slow:  marks tests as slow


"""
    借助Python的运算符号和assert关键字来实现的。
"""
import pytest


def test_kkk_01():
    print('kkk')
    # assert "预期结果" == "实际结果"
    assert 1 == 1 and 1 == 2
    """
        测试不相等 !=
        <=
        >=
        测试包含 in
        测试不包含 not in
        判断是否为true: is True
        判断是否不为true: is not True/is False
        and or
    """
@pytest.mark.smoke
def test_kkk_02():
    print('kkk')
    assert 1 == 2

def test_zzz_03():
    print('zzz')
    assert 1==1




if __name__ == '__main__':
    # pytest.main(['-s','-v','-k','zzz'])
    # pytest.main(['-s','-v','-k=zzz'])
    # pytest.main(['-vs'])
    # pytest.main(['-q'])
    # pytest.main(['-x'])
    # pytest.main(['-s','./doc/test_demo2.py::TestDemo::test_case_03'])
    # pytest.main(['-s','--junit-xml=./report/junit_report1.xml'])
    # pytest.main(['--maxfail=2'])
    pytest.main(['-m','smoke'])

import pytest


def test_case01():
    assert 1==1

def test_case02():
    assert 1==2


def test_case03():
    assert 1 == 3


def test_case04():
    assert 1 == 4


def test_case05():
    assert 1 == 5


def test_case06():
    assert 1 == 6

if __name__ == '__main__':
    # pytest.main(['test_many.py'])
    # 将测试执行发送到多个cpu;
    # pytest.main(['-n','2','test_many.py'])
    # 使用与计算机具有cpu内核一样多的进程
    pytest.main(['-n', 'auto', 'test_many.py'])

import pytest


def test_case01():
    assert 1==1

def test_case02():
    assert 1==2


def test_case03():
    assert 1 == 3


def test_case04():
    assert 1 == 4


def test_case05():
    assert 1 == 5


def test_case06():
    assert 1 == 6

if __name__ == '__main__':
    # 重新运行所有测试失败用例
    # pytest.main(['test_many.py'])
    # pytest.main(['--reruns', '3', 'test_rerun.py'])
    # 在每次重跑之间,增加一次延迟时间
    pytest.main(['--reruns', '3', '--reruns-delay','2','test_rerun.py'])

# 功能函数
import pytest


def multiply(a,b):
    return a * b


# if __name__ == '__main__':
#     print(multiply(1,2))


"""
    不含有类的用例预置和后置函数
    第一批次: setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行
    第二批次:setup_function/teardown_function:在每个测试函数之前与之后执行
    第三批次:setup/teardown:在每个测试函数之前与之后执行,在类中可以使用
    ps:执行的顺序是按优先级来的,改变方法位置结果也一样
"""

# 用例前置后置
def setup_module(module):
    print("setup_module========================")

def teardown_module(module):
    print("teardown_module=======================")

def setup_function(function):
    print("setup_function==========================")

def teardown_function(function):
    print("teardown_function==========================")

def setup():
    print("setup==========================")

def teardown():
    print("teardown==========================")


# ================测试用例==================
def test_01():
    print(multiply(3,4))

def test_02():
    print(multiply(4,4))

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

# 功能函数
import unittest

import pytest


def multiply(a,b):
    return a*b

class TestCase:

    """
        第一批次:setup_class/teardown_class:在当前的测试类的开始和结束时执行
        第二批次:setup_method/teardown_method:在每个测试方法开始与结束时执行
        第三批次:setup/teardown:在每个测试方法开始与结束时执行
        ps:执行的顺序按优先级来的,改变方法位置结果也一样
    """
    @classmethod
    def setup_class(cls):
        print("setup_class====================================")

    @classmethod
    def teardown_class(cls):
        print("teardown_class=====================================")


    def setup_method(self,method):
        print("setup_method===============================")

    def teardown_method(self, method):
        print("teardown_method===============================")

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

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


    #================测试用例=================
    def test01(self):
        print("类里的第一条用例")
        print(multiply(2,2))

# 测试函数
def test02():
    print("doc目录下的用例test02")

# 测试类以Test
class TestDemo:
    # 不能在pytest类中加构造方法
    # def __init__(self):
    #     print("构造方法")


    # 测试方法
    def test_case_03(self):
        print("类下的用例")

3.Pytest配置文件

pytest.ini
[pytest]
# 01 命令行参数,默认加到执行过程中
addopts = -s -v --html=./report/html_report.html
# 02 指定要运行的测试目录
testpaths = ./doc
# 03 指定要运行的测试文件规则
python_files = auto*.py
# 04 指定要运行的类名规则
python_classes = A* B*
# 05 指定要运行的测试用例方法/函数名
python_functions = ff*

import pytest


def test_case_01():
    print("test_case_01")

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

import pytest


def test_case_01():
    print("test_case_02")

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

import pytest


def test_case_03():
    print("test_case_03")

def ff01():
    print("ff01用例")

class A01:
    def test_aaa(self):
        print("A类里的用例")

    def ff02(self):
        print("A类里的用例ff02")

class B01:
    def test_bbb(self):
        print("B类里的用例")
    def ff03(self):
        print("B类里的用例ff03")

class Test01:
    def test_ccc(self):
        print("Test01类里的用例")
    def ff04(self):
        print("Test01类里的用例ff03")

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

4.Pytest高阶用法之跳过用例

1.无条件跳过

import pytest


@pytest.mark.skip(reason="不想跑了")
def test_01():
    print("用例1执行")


def test_02():
    print("用例2执行")

@pytest.mark.skip
def test_03():
    print("用例3执行")

if __name__ == '__main__':
    pytest.main(['-s'])

2.有条件跳过


import pytest




def test_02():
    print("用例2执行")
    return True


@pytest.mark.skipif(condition= 1>2,reason="不想跑了")
def test_01():
    print("用例1执行")


@pytest.mark.skip
def test_03():
    print("用例3执行")

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

5.函数数据参数化

1.单参数

import pytest


@pytest.mark.parametrize('a',['aaa','bbb','ccc'])
def test_01(a):
    print('\n'+a)

if __name__ == '__main__':
    pytest.main(['-s'])

2.多参数

import pytest


@pytest.mark.parametrize('username,pwd',[('zz','123456'),('xz','123456'),('qs','123456')])
def test_01(username,pwd):
    print('\n'+username)
    print('\n'+pwd)

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

3.函数式参数

import pytest

def return_data():
    return [('zz','123456'),('xz','123456'),('qs','123456')]

@pytest.mark.parametrize('data',return_data())
def test_01(data):
    print('\n'+data[0])
    print('\n'+data[1])

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

import pytest

# 常规list嵌套字典
# def return_data():
#     return [{'aaa':'1111'},{'bbb':'2222'},{'ccc','333'}]

# def return_data():
#     return [{'name':'zz','age':'18'},{'name':'xz','age':'20'},{'name':'qs','age':'18'}]


# def return_data():
#     return {'name':'zz','age':'18'}


# listt嵌套list也行
def return_data():
    return [[1,2,3,4,5],[2,2,2,2,2],[4,4,4,4,4]]


@pytest.mark.parametrize('data',return_data())
def test_01(data):
    print(data)
    print(type(data))
    print(data[0])
    print(data[1])
    print(data[2])



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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

司小幽

真诚赞赏,手留余香。

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

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

打赏作者

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

抵扣说明:

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

余额充值