zoukankan      html  css  js  c++  java
  • pytest-fixture

    fixtture使用:
    如何两个方法都是用同一个数据,则就可以使用fixture
        def test_1():
        json = requests.get("https://testerhome.com/api/v3/topics.json?limit=2").json()
        assert len(topics['topics']) == 2
    
        def test_2():
            json = requests.get("https://testerhome.com/api/v3/topics.json?limit=2").json()
            assert topics['topics'][0]['deleted'] == False
        俩个方法都用到json的数据,就可以使用fixture
        @pytest.fixture()
        def topics():
        url = 'https://testerhome.com/api/v3/topics.json?limit=2'
        return requests.get(url).json()
        def test_1(topics):
            assert len(topics['topics']) == 2
    
        def test_2(topics):
            assert topics['topics'][0]['deleted'] == False
    
        @pytest.fixture(scope="function")
        def topics():
        url = 'https://testerhome.com/api/v3/topics.json?limit=2'
        return requests.get(url).json()
        当scope="function"时,也是默认的,在每个方法前都会执行;
        当scope="module"时,每个模块都会执行一次
        当scope="session"时,同个目录下的模块都只执行一次
        当scope="class"时,    在每个类里面,只执行一次
    
        也可以将fixture的方法定义到conftest.py文件中,会自动读取
        python3 -m http.server 启动目录   会启动一个本地的服务
        
        生成allue的测试报告:
             pytest --alluredir=报告存放的路径  测试用例路径
             allure  serve 生产测试报告的路径   :可以打开allure测试报告
        生产Junit测试报告:
            pytest --junitxml=报告的路径  测试用例路径
        
        分组执行:pytest -m 组名
        @pytest.mark.a
        def test_2(topics2):
            assert topics2['topics'][0]['deleted'] == False
        @pytest.mark.b
        def test_3(topics3):
            assert topics3['topics'][0]['deleted'] == False
       @pytest.mark.b
        def test_4(topics3):
            assert topics3['topics'][0]['deleted'] == False
    
        pytest -m b执行b组的测试用例
    

    pytest常用的参数

    -v:输出的信息会更详细,最明显的区别就是每个文件中的测试用例都占一行,测试的名字和结果都会显示出来
        pytest -v file.py
    -m: 用于标记测试分组,以便快速选中并运行 ,下面例子可以使用:pytest -m "run_bb"只执行test_case_o1
            @pytest.mark.run_bb
            def test_case_01():
            """
            出口流程可以正常创建
            """
            assert 0
    
            @pytest.mark.run_aa
            def test_case_02():
                """
                入库流程可以正常创建
                """
                assert 0 == 0
    --collect-only:在测试之前,可以展示在给定配置下那些测试用例会被执行,检查选中的测试用例是否符合预期
    -k:允许你使用表达式指定希望允许的测试用例,下面的例子可以这样允许:pytest -k "aaa or bbb"
            两个py文件:test_aaa.py和test_bbb.py
    -x:一旦遇到失败,就会全局停止
    --tb=style:决定捕捉到失败是输出信息的显示方式,pytest会列举失败信息,包括失败出现在哪一行、是什么失败、怎么失败的,此过程我们称之为”信息回溯“
        一般用的是:--tb=no  屏蔽全部的回溯信息
    -q:简化输出信息
    -s:在终端允许时输出某些结果,正常情况下,所有的测试输出都会被捕捉
    
  • 相关阅读:
    Android系统编程入门系列之应用权限的定义与申请
    Android系统编程入门系列之应用间数据共享ContentProvider
    微服务与架构师
    一个C#开发者用Java搭建Android框架的心路历程
    学习使用Wpf开源的文本编辑器—smithhtmleditor
    聊聊 Kubernetes Pod or Namespace 卡在 Terminating 状态的场景
    写给自己看的的密码学知识
    pymongo基础操作
    1103-词牌名,合称,诗词形式
    1102-诗词类别补充与pyhanlp探索
  • 原文地址:https://www.cnblogs.com/an5456/p/11229218.html
Copyright © 2011-2022 走看看