首页 > 其他分享 >pytest 学习 - 01 setup与teardown

pytest 学习 - 01 setup与teardown

时间:2024-05-12 22:43:55浏览次数:17  
标签:teardown setup print pytest test class def

前言

与unitest一样,python也拥有前置(setup)、后置操作(teardown)操作,而且更加的灵活

setup与teardown介绍

1. 模块级别(setup_module / teardown_module)模块始末,全局的(优先最高)
2. 函数级(setup_function / teardown_function)只对函数用例生效 (不在类中)
3. 类级 (setup_class / teardown_class)只在类中前后运行一次 (在类中)
4. 方法级 (setup_method / teardown_method)开始于方法始末 (在类中)
5. 类里面 (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("这是一个外部的方法")
    assert "测栈云" == "测栈云"


class TestDemo:

    def setup_class(self):
        print("类级别的setup_class")

    def teardown_class(self):
        print("类级别的teardown_class")

    def setup(self):
        print("这是一个setup")

    def teardown(self):
        print("这是一个teardown")

    def test_a(self):
        assert 1 == 1

    def test_b(self):
        assert 2 == 2

    def test_c(self):
        assert 3 == 3


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

============================= test session starts =============================
collecting ... collected 4 items

main.py::test_login 模块级别的setup_module
函数级别的setup_function
PASSED                                               [ 25%]这是一个外部的方法
函数级别的teardown_function

main.py::TestDemo::test_a 类级别的setup_class
这是一个setup
PASSED                                         [ 50%]这是一个teardown

main.py::TestDemo::test_b 这是一个setup
PASSED                                         [ 75%]这是一个teardown

main.py::TestDemo::test_c 这是一个setup
PASSED                                         [100%]这是一个teardown
类级别的teardown_class
模块级别的teardown_module


======================== 4 passed, 6 warnings in 0.05s ========================

  

 

标签:teardown,setup,print,pytest,test,class,def
From: https://www.cnblogs.com/quxue/p/18188328

相关文章

  • pytest + yaml 框架 -69.新增depend 关键字,导入其它yaml用例
    前言有小伙伴提到,test_a.yml中已经写了几个流程,test_b.yml中希望能接着test_a.yml去写用例,于是就需要先导入test_a.yml用例。为了满足此需求,v1.6.3版本在config中新增depend关键字。需求场景test_a.yml中已经写了一些用例config:variables:x:helloy:wo......
  • Error: Cannot find module ‘D:\SoftSetupLoaction\nodejs\node_global\node_mod
    Error:Cannotfindmodule‘D:\SoftSetupLoaction\nodejs\node_global\node_modules\npm\bin\npm-cli.js‘  出现原因:重新安装可装了nodejs和npm网上查了很多方法,都建议重装,但是都没有效果(因为我就是重装之后出现的问题)按照错误提示node_global找不到npm-cli.js,个......
  • 09-初始setup-axios-promise
    监听属性需要使用ref和ractive包一下才可以正常监听。//使用相关属性需要导入import{reactive,ref,watch}from"vue";//监听单个letname=ref("阿珂")watch(name,(newValue,oldValue)=>{console.log("老名字",oldValue);console.log("新名字"......
  • Vue 3 setup
    【一】setup函数setup函数的设计是为了使用vue3的组合式api,setup函数必须要有返回值,在里面定义的变量必须要返回出去才能在html里面使用【1】定义变量setup(){//1.定义变量跟正常写js是一样的letname='hqq'letage=18//setup函数必须要有返回值......
  • pytest 学习 - 00 环境安装配置
    前言pytest是一个非常好用且成熟的全功能Python测试框架,个人觉得比传统的Unitest好多用了,现在面试如果写只会Unitest会被鄙视的。主要有以下特点:1.简单灵活,容易上手,参数化灵活。2.测试用例支持很多机制像skip、xfail、自动失败重试等处理。3.能够......
  • pytest lastfailed原理
    相信很多使用pytest的,都知道pytest有运行上次失败用例的参数,如下:--lf,--last-failedrerunonlytheteststhatfailedatthelastrun(orallifnonefailed)--ff,--failed-firstrunalltests,butrunthelastfailuresfirst.Thism......
  • python之pytest
    安装pipinstallpytest 终端运行pytest:在当前目录中查找并运行所有符合测试用例命名规范的测试用例。pytest-q:以简化模式运行所有测试用例。pytest-v:以详细模式运行所有测试用例。pytesttest_sample.py:运行指定的测试文件test_sample.py中的所有测试用例。......
  • python+appium+pytest做app自动化测试
    我在另一篇博客中写了使用unittest做app自动化测试的,包含了前期的环境的环境搭建,请参考如下链接:python+appium+unittest做app自动化测试这里,我们使用pytest框架再改写一个版本,因为pytest做测试报告看着更加好看,代码改良如下:fromappiumimportwebdriverimportpytest@pytest......
  • pytest + yaml 框架 -68.新增全局请求参数配置verify和headers
    前言最近有小伙伴提到如何全局添加请求参数verify=False和全局请求添加头部参数如:{"x-token":"xxx"}之前的版本可以用fixture解决,v1.5.8版本可以支持在config中配置fixture更新全局请求第一种解决方案,通过fixture来更新全局session会话[email protected](s......
  • pytest多线程运行控制台日志输出异常
    开启多线程后控制台日志显示错误,但是日志文件输出正确百度了一个晚上也没有解决,AI也问不出来解决办法,希望有大佬看到。开启多线程运行用例单独运行只有一个线程【gw1】输出日志信息。【gw2,gw0,gw3】都不能输出日志信息通过main()方式运行,控制台日志信息乱码......