pytest的setup和teardown

在unittest中就有setup和teardown,pytest也不例外。主要有五种:

  • 模块级(setup_module/teardown_module),开始于模块始末,全局的
  • 函数级(setup_function/teardown_function),只对函数用例生效(不在类中)
  • 类级(setup_class/teardown_class),只在类中前后运行一次(在类中)
  • 方法级(setup_method/teardown_method),开始于方法前后(在类中)
  • setup/teardown运行在调用方法/函数的前后

例如:下面的test_setup.py测试了上面五种方法,且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_01():
    print('test01')

def test_02():
    print('test02')

class TestSet():
    @classmethod
    def setup_class(cls):
        print('setup_class')

    @classmethod
    def teardown_class(cls):
        print('teardown_class')

    def test_method1(self):
        print('test_method1')

    def test_method2(self):
        print('test_method2')

    def setup_method(self):
        print('setup_method')


    def teardown_method(self):
        print('teardown_method')


def setup():
    print('setup')

def teardown():
    print('teardown')

输出为:

D:\pythonProject\my_selenium_project\testcases\pytest>pytest -v -s  test_setup.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\my_selenium_project\testcases\pytest, configfile: pytest.ini
collected 4 items                                                                                                                                                    

test_setup.py::test_01 setup_module
setup_function
setup
test01
PASSEDteardown
teardown_function

test_setup.py::test_02 setup_function
setup
test02
PASSEDteardown
teardown_function

test_setup.py::TestSet::test_method1 setup_class
setup_method
test_method1
PASSEDteardown_method

test_setup.py::TestSet::test_method2 setup_method
test_method2
PASSEDteardown_method
teardown_class
teardown_module

setup/teardown在类中:

D:\pythonProject\my_selenium_project\testcases\pytest>pytest -v -s  test_setup.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\my_selenium_project\testcases\pytest, configfile: pytest.ini
collected 4 items                                                                                                                                                    

test_setup.py::test_01 setup_module
setup_function
test01
PASSEDteardown_function

test_setup.py::test_02 setup_function
test02
PASSEDteardown_function

test_setup.py::TestSet::test_method1 setup_class
setup_method
setup
test_method1
PASSEDteardown
teardown_method

test_setup.py::TestSet::test_method2 setup_method
setup
test_method2
PASSEDteardown
teardown_method
teardown_class
teardown_module


========================================================================= 4 passed in 0.04s =========================================================================

### 如何在个人电脑上安Ubuntu 20.04 LTS #### 准备工作 对于希望在同一台计算机上运行Windows和Ubuntu的操作系统的用户来说,了解一些基本概念是非常重要的。这不仅有助于理解整个安过程,还能帮助解决可能出现的问题[^2]。 #### 更改硬盘模式 由于不同的硬盘模式可能影响Ubuntu的正常安,在开始安前应确认硬盘处于兼容模式。例如,RAID模式下的硬盘可能导致Ubuntu无法成功安;因此,建议将硬盘模式更改为AHCI以确保顺利安Ubuntu系统[^1]。 #### 分区准备 为了给Linux操作系统腾出足够的空间,可以在现有的Windows分区中划分一部分作为Ubuntu专用的空间。具体操作可以通过进入`此电脑->管理->磁盘管理`界面,选择一块具有足够空闲容量的磁盘,接着右击该磁盘并选择“压缩卷”。等待一段间直到创建了一个未分配区域用于后续安Ubuntu系统[^3]。 #### 获取ISO镜像文件 获得官方发布的Ubuntu ISO映像文件是启动安的第一步。虽然可以从多个渠道获取这些文件,但是最可靠的方法是从官方网站或其他可信来源下载最版本的Ubuntu发行版。如果遇到网络条件不佳的情况,则可以选择从国内云存储平台如百度网盘搜索由社区成员共享的相关资源,并依照指引完成下载任务[^4]。 #### 创建可引导U盘 一旦拥有了所需的ISO文件之后,下一步就是制作一个能够用来启动计算机并执行实际安工作的USB闪存驱动器。推荐使用 Rufus 或 Etcher 这样的工具来简化这个过程: ```bash sudo dd bs=4M if=/path/to/ubuntu.iso of=/dev/sdX status=progress && sync ``` 注意替换 `/path/to/ubuntu.iso` 和 `/dev/sdX` 成具体的路径名与设备名称。 #### 开始安 重启机器并设置BIOS/UEFI优先级让其从刚才制好的U盘启动。根据屏幕上的指示一步步前进直至到达语言选择页面。这里可以选择尝试不改变任何东西的情况下体验Live CD中的Ubuntu桌面环境,也可以直接点击“Install Ubuntu”。 在整个过程中会询问有关键盘布局、区设定等问题,同也会给出选项让用户决定如何处理现有数据——既可以全覆盖旧有的操作系统,也能保留当前的数据实现双系统共存配置。 #### 完成安 当所有的设置都完成后,只需耐心等待进度条走完即可。期间可能会被要求输入用户名密码等个人信息以便日后登录账户之用。最后一步便是移除外部介质(即之前使用的U盘),再次按下电源按钮使硬件重初始化从而正式加载刚刚部署完毕的鲜出炉的Ubuntu 20.04 LTS!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

永远不要矫情

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值