首页 > 其他分享 >Pytest - setup 和 teardown

Pytest - setup 和 teardown

时间:2023-05-25 15:12:40浏览次数:38  
标签:teardown setup test Pytest print class def

Pytest - setup 和 teardown


  • 执行用例肯定有些需要前置条件或后置操作,例如前置的用户登陆,后置的清理数据等操作;

  • unittest提供了两种前置(setup、setupClass)和两种后置(teardown、teardownClass);

  • 相比之下,pytest 提供了十种 setupteardown 方法:

    • 模块级别:setup_module、teardown_module

    • 函数级别:setup_function、teardown_function,不在类中的方法

    • 类级别:setup_class、teardown_class

    • 方法级别:setup_method、teardown_method

    • 用例级别:setup、teardown


test_py.py

import pytest

def setup_module():
    print("\n!!!! setup_module > 整个.py模块开始前只执行一次:打开浏览器/获取cookie !!!!")

def teardown_module():
    print("!!!! teardown_module > 整个.py模块结束后只执行一次:关闭浏览器 !!!!")


def setup_function():
    print("\n### setup_function > 每个函数级别用例开始前都执行 ###")

def teardown_function():
    print("### teardown_function > 每个函数级别用例结束后都执行 ###")


def test_one():
    print("test case 1")

def test_two():
    print("test case 2")


class TestCase():
    def setup_class(self):
        print("\n^^^ setup_class > 整个测试类开始前只执行一次 ^^^")

    def teardown_class(self):
        print("^^^ teardown_class > 整个测试类结束后只执行一次 ^^^")

    def setup_method(self):
        print("\n=== setup_method > 类里面每个用例执行前都会执行 ===")

    def teardown_method(self):
        print("=== teardown_method > 类里面每个用例结束后都会执行 ===")

    def setup(self):
        print("--- setup > 类里面每个用例执行前都会执行 ---")

    def teardown(self):
        print("--- teardown > 类里面每个用例结束后都会执行 ---")


    def test_three(self):
        print("test case 3")

    def test_four(self):
        print("test case 4")

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

  • 执行结果如图所示各级别(讲解顺序从上往下,级别从低到高):

    • 黄色框:用例级别 【setup、teardown】

    • 橙色框:方法级别【setup_method、teardown_method】

    • 蓝色框:类级别【setup_class、teardown_class】

    • 绿色框:函数级别【setup_function、teardown_function】

    • 红色框:模块级别【setup_module、teardown_module】

    image-20220922173920314

标签:teardown,setup,test,Pytest,print,class,def
From: https://www.cnblogs.com/mzline/p/17431289.html

相关文章

  • Pytest - 断言判断(2) - 断言失败继续执行(pytest-assume)
    断言失败继续执行前言一般情况下我们在使用assert断言失败后,后面的代码就不会继续运行;如果我们想要在断言失败后想要继续运行代码,就不能使用assert进行验证;一个可以允许pytest测试用例中,执行多个失败的断言的插件:多重断言pytest-assume安装pipinstallpytest......
  • Pytest - pytest 命令(1) - 命令执行方法
    命令执行方法讲解下pytest分别在Windows,Linux,Pycharm中,执行pytest的方法;追加的参数可以参考:Pytest-pytest命令(2)-命令参数及含义Pytest-pytest命令(3)-常用命令的使用Windows执行Windows下执行pytest测试脚本没什么难度,在用例的目录下打开cmd窗口......
  • Setup安装在VS设置
    Nuget安装InstallerProjects后 在需要做安装的项目文件里新建安装包项目,然后在ApplicationFolder右击Add文件,如下图winform的debug文件全部选定: ApplicationFolder再点Add“项目输出”: 右击新生成的主输出文件->Create Shortcut to主输出fromtest(Active)依赖......
  • vue中<script setup>中使用watch、computed、props、defineExpose、defineEmits等方法
    <scriptsetup>是在单文件组件(SFC)中使用组合式API的编译时语法糖。相比于普通的<script>语法,它具有更多优势:更少的样板内容,更简洁的代码。能够使用纯TypeScript声明props和抛出事件。更好的运行时性能(其模板会被编译成与其同一作用域的渲染函数,没有任何的中间......
  • pytest + yaml 框架 -29.模板过滤器语法与自定义过滤器使用
    前言v1.2.6版本支持模板过滤器语法的使用,并且可以自定义过滤器了。针对有同学提到上个接口返回一个id值,下个接口引用变量的时候需要根据这个值做一些运算,比如在引用的结果加1.jinja2是可以支持模板过滤器语法的,本篇介绍下模板过滤器的相关使用.v1.2.6版本主要更新以下几点1......
  • pytest command line
       pytest-v-s--last-failed--alluredir=report/xml -v打印详细的信息 -s输出print打印信息 --last-failed只执行上次失败的用例 --alluredir=report/xml输出alluredir到report/xml......
  • APP自动化--pytest-把图片添加到测试报告中--(异常截图)
    前面在 APP自动化--pytest-把图片添加到测试报告中--(主动截图)中说明了extra.append(extras.image(driver1.get_screenshot_as_base64()))的用法,那么把它放到conftest的异常截图代码中就可以实现异常截图。importpytestfrompytest_htmlimportextras"""解决pytest-html......
  • Pytest - pytest 命令(3) - 常用命令的使用
    pytest常用命令测试信息输出#设置pytest的执行参数"-q":安静模式,不输出环境信息pytest.main(["-q"])#设置pytest的执行参数"-s":显示程序中的print/logging输出pytest.main(["-s"])#设置pytest的执行参数"-v":丰富信息模式,输出更详细的用例执行信息pytest.main(......
  • Pytest - pytest运行常用命令参数
    pytest运行常用命令参数参数含义-q输出的结果缩短显示-s显示在python程序中的print输出的内容,如是不加则不会显示--durations获取最慢的n个用例的执行耗时--durations-min此参数和–durations边用,表示在–durations-min时间段之内,获取最慢的测例耗时-......
  • APP自动化--pytest-把图片添加到测试报告中
    pytest输出测试报告时,有时候需要把截图插入报告中,操作可以如下用例如下:frompytest_htmlimportextras导入包#!/usr/bin/envpython#-*-coding:utf-8-*-#@Time:2023/5/1217:44#@Author:gezirui#@File:test_dsw_app_ss_011_截图实现.py#@Softw......