移动端自动化测试05pytest

pytest安装
pip3 install -U pytest
pytest的main运行方式

import pytest
def test_a():
    print(">>>>>>>>>test_a")
    assert True
def test_b():
    print(">>>>>>>>>test_a")
    assert False
if __name__ == '__main__':
    #pytest main运行方式
    pytest.main(["-s","test001.py"])

插件名称:pytest-html —生成报告
安装方式:
1.安装包方式 python setup.py install
2.命令行 pip3 install pytest-html
使用方法:
命令行格式:pytest --html=用户路径/report.html

运行方式:
1.修改Test_App/pytest.ini文件,添加报告参数,即:addopts = -s --html=./report.html
# -s:输出程序运行信息
# --html=./report.html 在当前目录下生成report.html文件
⚠️ 若要生成xml文件,可将–html=./report.html 改成 --html=./report.xml
2.命令行进入Test_App目录
3.执行命令: pytest
执行结果:
1.在当前目录会生成assets文件夹和report.html文件
插件名称:pytest-ordering —控制函数的执行顺序
安装方式:
1.安装包方式 python setup.py install
2.命令行 pip3 install pytest-ordering
使用方法:
1.标记于被测试函数,@pytest.mark.run(order=x)
2.根据order传入的参数来解决运行顺序
3.order值全为正数或全为负数时,运行顺序:值越小,优先级越高
4.正数和负数同时存在:正数优先级高
插件名称:pytest-rerunfailures ----失败重试
安装方式:
1.安装包方式 python setup.py install
2.命令行 pip3 install pytest-rerunfailures
使用方法:
命令行格式:pytest --reruns n # n:为重试的次数
pytest的配置文件—pytest.ini

[pytest]
# 命令行参数 ---失败重试1次,报告放在report文件夹,报告名为report
addopts = -s --reruns 1 --html=./report/report.html
# 测试路径	---测试脚本的路径文件夹
testpaths = ./scripts_test
# 搜索文件名	---找test_开始的文件名
python_file = test_*.py
# 搜索测试类名	---Test_开始的类名
python_classes = Test_*
#搜索测试方法名	---test_开始的方法名
python_functions = test_*

fixture
方法:fixture(scope=“function”, params=None, autouse=False, ids=None, name=None)
常用参数:
scope:被标记方法的作用域
function" (default):作用于每个测试方法,每个test都运行一次
“class”:作用于整个类,每个class的所有test只运行一次
“module”:作用于整个模块,每个module的所有test只运行一次
"session:作用于整个session(慎用),每个session只运行一次
params:(list类型)提供参数数据,供调用标记方法的函数使用
autouse:是否自动运行,默认为False不运行,设置为True自动运行
通过参数引用

import pytest
class Test_ABC:
    @pytest.fixture()
    def before(self):
        print("------->before")
    def test_a(self,before): # ⚠️ test_a方法传入了被fixture标识的函数,已变量的形式
        print("------->test_a")
        assert 1
if __name__ == '__main__':
    pytest.main(["-s","test_abc.py"])

注:发现before自动优先于测试类运行
通过函数引用

import pytest
@pytest.fixture() # fixture标记的函数可以应用于测试类外部
def before():
  print("------->before")
@pytest.mark.usefixtures("before")
class Test_ABC:
  def setup(self):
      print("------->setup")
  def test_a(self):
      print("------->test_a")
      assert 1
if __name__ == '__main__':
    pytest.main(["-s","test_abc.py"])

注:发现before自动优先于测试类运行
通过默认设置运行

import pytest
@pytest.fixture(autouse=True) # 设置为默认运行
def before():
    print("------->before")
class Test_ABC:
    def setup(self):
        print("------->setup")
    def test_a(self):
        print("------->test_a")
        assert 1
if __name__ == '__main__':
    pytest.main(["-s","test_abc.py"])

注:发现before自动优先于测试类运行
设置作用域为function

import pytest
@pytest.fixture(scope='function',autouse=True) # 设置为默认运行
def before():
    print("------->before")
class Test_ABC:
    def setup(self):
        print("------->setup")
    def test_a(self):
        print("------->test_a")
        assert 1

    def test_b(self):
        print("------->test_b")
        assert 1
if __name__ == '__main__':
    pytest.main(["-s","test_abc.py"])

注:发现before运行了两次
设置作用域为class

import pytest
@pytest.fixture(scope='class',autouse=True) # 设置为默认运行
def before():
    print("------->before")
class Test_ABC:
    def setup(self):
        print("------->setup")
    def test_a(self):
        print("------->test_a")
        assert 1

    def test_b(self):
        print("------->test_b")
        assert 1
if __name__ == '__main__':
    pytest.main(["-s","test_abc.py"])

before只运行一次
返回值

import pytest
@pytest.fixture()
def need_data():
    return 2  # 返回数字2

class Test_ABC:
    def test_a(self, need_data):
        print("------->test_a")
        assert need_data != 3  # 拿到返回值做一次断言

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

下面这个会发现结果运行了三遍

import pytest
@pytest.fixture(params=[1, 2, 3])
def need_data(request): # 传入参数request 系统封装参数
  return request.param # 取列表中单个值,默认的取值方式

class Test_ABC:

  def test_a(self,need_data):
      print("------->test_a")
      assert need_data != 3 # 断言need_data不等于3

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

跳过测试函数
方法:
skipif(condition, reason=None)
参数:
condition:跳过的条件,必传参数
reason:标注原因,必传参数
使用方法:
@pytest.mark.skipif(condition, reason=“xxx”)
标记为预期失败函数
方法:
xfail(condition=None, reason=None, raises=None, run=True, strict=False)
常用参数:
condition:预期失败的条件,必传参数
reason:失败的原因,必传参数
使用方法:
@pytest.mark.xfail(condition, reason=“xx”)
函数数据参数化
方法:
parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)
常用参数:
argnames:参数名
argvalues:参数对应值,类型必须为list
当参数为一个时格式:[value]
当参数个数大于一个时,格式为:[(param_value1,param_value2…),(param_value1,param_value2…)]
使用方法:
@pytest.mark.parametrize(argnames,argvalues)
⚠️ 参数值为N个,测试方法就会运行N次
单个参数示例:

  import pytest
    class Test_ABC:
        def setup_class(self):
            print("------->setup_class")
        def teardown_class(self):
            print("------->teardown_class")

        @pytest.mark.parametrize("a",[3,6]) # a参数被赋予两个值,函数会运行两遍
        def test_a(self,a): # 参数必须和parametrize里面的参数一致
            print("test data:a=%d"%a)
            assert a%3 == 0
执行结果:
    test_abc.py 
    ------->setup_class
    test data:a=3 # 运行第一次取值a=3
    .
    test data:a=6 # 运行第二次取值a=6
    . 
    ------->teardown_class

多个参数示例:

     import pytest
    class Test_ABC:
        def setup_class(self):
            print("------->setup_class")
        def teardown_class(self):
            print("------->teardown_class")

        @pytest.mark.parametrize("a,b",[(1,2),(0,3)]) # 参数a,b均被赋予两个值,函数会运行两遍
        def test_a(self,a,b): # 参数必须和parametrize里面的参数一致
            print("test data:a=%d,b=%d"%(a,b))
            assert a+b == 3
执行结果:
    test_abc.py 
    ------->setup_class
    test data:a=1,b=2 # 运行第一次取值 a=1,b=2
    .
    test data:a=0,b=3 # 运行第二次取值 a=0,b=3
    .
    ------->teardown_class
    函数返回值类型示例:
 import pytest
    def return_test_data():
        return [(1,2),(0,3)]
    class Test_ABC:
        def setup_class(self):
            print("------->setup_class")
        def teardown_class(self):
            print("------->teardown_class")

        @pytest.mark.parametrize("a,b",return_test_data()) # 使用函数返回值的形式传入参数值
        def test_a(self,a,b):
            print("test data:a=%d,b=%d"%(a,b))
            assert a+b == 3
执行结果:
    test_abc.py 
    ------->setup_class
    test data:a=1,b=2 # 运行第一次取值 a=1,b=2
    .
    test data:a=0,b=3 # 运行第二次取值 a=0,b=3
    .
    ------->teardown_class
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytestPython中一个常用的测试框架,可以用于接口自动化测试。在pytest接口自动化的面试中,可能会涉及以下几个方面的问题: 1. 你是如何使用pytest进行接口自动化测试的? 你可以回答说,在pytest中,你可以使用pytest框架提供的装饰器来标记测试用例,使用assert语句来进行断言,并且可以使用pytest的fixture功能来管理测试数据和测试环境的准备和清理。 2. 你如何处理上下游接口的数据依赖? 你可以回答说,在接口自动化测试中,上下游接口之间可能存在数据依赖关系。你可以使用pytest框架的fixture功能来准备测试数据,并通过参数化来传递数据给测试用例。另外,你还可以使用pytest的钩子函数来处理接口之间的顺序和依赖关系。 3. 你在接口自动化测试中使用过哪些工具和技术? 你可以回答说,在接口自动化测试中,你可能使用过工具如Requests来发送HTTP请求,使用JsonPath或XPath来解析响应数据,使用Pytest-html或Allure生成测试报告等。另外,你还可以提到一些常用的接口测试技术如参数化、数据驱动、Mock等。 4. 你在接口自动化测试项目中的经验是什么? 你可以回答说,在接口自动化测试项目中,你可能参与过Web UI自动化测试、接口自动化测试、性能自动化测试移动端自动化测试等不同类型的项目。你可以描述你使用的工具和技术,以及你遇到的挑战和解决方案。 5. 什么是PO模式? 你可以回答说,PO(Page Object)模式是一种在测试框架中常用的设计模式,用于将页面的元素、操作和断言封装为一个个Page Object,通过调用Page Object来实现测试用例的可维护性和可重用性。PO模式可以有效地降低测试用例的耦合度,并提高代码的可读性和可维护性。 通过回答这些问题,你可以展示你对pytest框架的理解和应用能力,以及你在接口自动化测试项目中的实际经验。同时,你还可以提到一些测试技术和设计模式,展示你的专业知识和技能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python 接口自动化测试-----常见面试题汇总](https://blog.csdn.net/weixin_56331124/article/details/129381618)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python自动化测试面试题精选(一)](https://blog.csdn.net/weixin_57805858/article/details/131207092)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值