Pytest系列(3) - setup和teardown的详细使用

简介: Pytest系列(3) - setup和teardown的详细使用

如果你还想从头学起Pytest,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1690628.html

 

前言


用过unittest的童鞋都知道,有两个前置方法,两个后置方法;分别是

  • setup()
  • setupClass()
  • teardown()
  • teardownClass()

Pytest也贴心的提供了类似setup、teardown的方法,并且还超过四个,一共有十种

  • 模块级别:setup_module、teardown_module
  • 函数级别:setup_function、teardown_function,不在类中的方法
  • 类级别:setup_class、teardown_class
  • 方法级别:setup_method、teardown_method
  • 方法细化级别:setup、teardown

 

代码


用过unittest的童鞋,对这个前置、后置方法应该不陌生了,我们直接来看代码和运行结果

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__  =
__Time__   = 2020-04-06 11:40
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import pytest
def setup_module():
    print("=====整个.py模块开始前只执行一次:打开浏览器=====")
def teardown_module():
    print("=====整个.py模块结束后只执行一次:关闭浏览器=====")
def setup_function():
    print("===每个函数级别用例开始前都执行setup_function===")
def teardown_function():
    print("===每个函数级别用例结束后都执行teardown_function====")
def test_one():
    print("one")
def test_two():
    print("two")
class TestCase():
    def setup_class(self):
        print("====整个测试类开始前只执行一次setup_class====")
    def teardown_class(self):
        print("====整个测试类结束后只执行一次teardown_class====")
    def setup_method(self):
        print("==类里面每个用例执行前都会执行setup_method==")
    def teardown_method(self):
        print("==类里面每个用例结束后都会执行teardown_method==")
    def setup(self):
        print("=类里面每个用例执行前都会执行setup=")
    def teardown(self):
        print("=类里面每个用例结束后都会执行teardown=")
    def test_three(self):
        print("three")
def test_four(self):
        print("four")
if __name__ == '__main__':
    pytest.main(["-q", "-s", "-ra", "setup_teardown.py"])


执行结果

image.png



相关文章
|
10月前
|
缓存 测试技术
31-pytest-内置fixture之cache使用
31-pytest-内置fixture之cache使用
31-pytest-内置fixture之cache使用
33-pytest-内置fixture之pytestconfig使用
33-pytest-内置fixture之pytestconfig使用
|
5月前
|
测试技术 Python
pytest中的fixture和conftest.py
pytest中的fixture和conftest.py
|
9月前
|
测试技术 Python
pytest--fixture
pytest--fixture
|
10月前
|
测试技术
03-pytest-测试用例setup和teardown
03-pytest-测试用例setup和teardown
|
10月前
|
Web App开发 测试技术
10-pytest-parametrize中使用fixture
10-pytest-parametrize中使用fixture
|
10月前
|
测试技术
32-pytest-内置fixture之request使用
32-pytest-内置fixture之request使用
|
12月前
|
测试技术
pytest conftest.py和fixture的配合使用
pytest conftest.py和fixture的配合使用
|
测试技术
pytest学习和使用9-fixture中conftest.py如何使用?
pytest学习和使用9-fixture中conftest.py如何使用?
104 0
pytest学习和使用9-fixture中conftest.py如何使用?