pytest-用例运行级别 class级

复制代码
'''
模块级(setup_module/teardown_module)开始于模块始末,
全局的在类中不起作用
类级(setup_class/teardown_class)只在类中前后运行一次(在
类中)
方法级(setup_method/teardown_method)开始于方法始末
(在类中)
 
函数级(setup_function/teardown_function只对函数用例生
效(在类中不生效)
setup_function
teardown_function
'''
import  pytest
def setup_function():
    print()
    print("setup_function:class外的每个用例前开始执行")
 
def teardown_function():
    print("teardown_function:class外的每个个用例后开始执行")
 
def setup_module():
    """
    这是一个module级别的setup,它会在本module(test_fixt_class.py)里
    所有test执行之前,被调用一次。
    注意,它是直接定义为一个module里的函数"""
    print()
    print("-------------- setup before module --------------")
def teardown_module():
    """
    这是一个module级别的teardown,它会在本module(test_fixt_class.py)里
    所有test执行完成之后,被调用一次。
    注意,它是直接定义为一个module里的函数"""
    print("-------------- teardown after module --------------")
def test_add():
    print("正在执行test_fire")
    a = "hello"
    b = "hello word"
    assert a in b
class TestCase():
    def setup(self):
        print("setup: 每个用例开始前执行")
    def teardown(self):
        print("teardown: 每个用例结束后执行")
    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 test_one(self):
        print("正在执行----test_one")
        x = "this"
        assert 'h' in x
    def test_three(self):
        print("正在执行test_two")
        a = "hello"
        b = "hello word"
        assert a in b
    def add(self,a, b):
        print("这是加减法")
        return a + b
if __name__ == '__main__':
    pytest.main(['-s', 'test_fixt_class'])
复制代码

运行结果:

复制代码
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.1.0, py-1.8.0, pluggy-0.12.0
rootdir: E:\py_pytest\interfacecollected 3 items

test_fixt_class.py
-------------- setup before module --------------

setup_function:class外的每个用例前开始执行
.正在执行test_fire
teardown_function:class外的每个个用例后开始执行
setup_class:所有用例执行之前
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
.正在执行----test_one
teardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
.正在执行test_two
teardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
teardown_class:所有用例执行之前
-------------- teardown after module --------------
[100%]

============================== 3 passed in 0.02s ==============================
复制代码
    1. 结论1
    2. 模块级(setup_module/teardown_module)开始于模块始末,
    3. 全局的在类中不起作用
    4. 函数级(setup_function/teardown_function只对函数用例生
    5. 效(在类中不生效)
    6.  
    7. 结论2
    8. 从结果看出,运行的优先级:
    9. setup_model>setup_class>setup_method> setup >用例case> teardown> teardown_method> teardown_class >teardown_model
    10.  
    11. 结论3
    12.  从运行结果看出, setup_module/teardown_module 的优先级
    13.  是最大的,然后函数里面用到的 setup_function/teardown_function
    14.  不类里面的 setup_class/teardown_class 互不干涉

 

设置全局mysql,用例执行完之后关闭

conftest.py

复制代码
import pytest
from selenium import webdriver
@pytest.fixture
def dri():
    print("打开浏览器")
    driver=webdriver.Chrimport pytest
from selenium import webdriver
@pytest.fixture
def dri():
    print("打开浏览器")
    driver=webdriver.Chrome()
    return driver
@pytest.fixture(autouse=True)
def driver_quit(dri):
    yield
    print("test_1执行完毕,关闭浏览器")
    dri.quit()
复制代码

 

posted @   一只琥珀  阅读(289)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示