zoukankan      html  css  js  c++  java
  • pytest_用例运行级别_函数级

    '''
     函数级(setup_function/teardown_function只对函数用例生
    效(不在类中)在类中是用该方法不生效
    '''
    import  pytest
    def setup_module():
        """
        这是一个module级别的setup,它会在本module(test_fixt_function.py)里
        所有test执行之前,被调用一次。
        注意,它是直接定义为一个module里的函数"""
        print()
        print("-------------- setup before module --------------")
    def teardown_module():
        """
        这是一个module级别的teardown,它会在本module(test_fixt_function.py)里
        所有test执行完成之后,被调用一次。
        注意,它是直接定义为一个module里的函数"""
        print("-------------- teardown after module --------------")
    
    def setup_function():
        print()
        print("setup_function:每个用例前开始执行")
    
    def teardown_function():
        print("teardown_function:没个用例后开始执行")
    
    def test_one():
        print("正在执行test_one")
        x = "hello word"
        assert  "h" in x
    
    def test_two():
        print("正在执行test_two")
        a = "hello"
        b = "hello word"
        assert  a in b
    if __name__ == '__main__':
        pytest.main(['-s','test_fixt_function.py'])

    运行结果:


    ============================= test session starts =============================
    platform win32 -- Python 3.7.4, pytest-5.1.0, py-1.8.0, pluggy-0.12.0
    rootdir: E:py_pytestinterfacecollected 2 items

    test_fixt_function.py
    -------------- setup before module --------------

    setup_function:每个用例前开始执行
    .正在执行test_one
    teardown_function:没个用例后开始执行

    setup_function:每个用例前开始执行
    .正在执行test_two
    teardown_function:没个用例后开始执行
    -------------- teardown after module --------------
    [100%]

    ============================== 2 passed in 0.02s ==============================

  • 相关阅读:
    python高级语法
    python的内置类型
    Python现状
    前端项目技术栈
    1.22
    类型转换!
    文件上传下载!
    Struts2的Ognl详解
    第二章复习
    解耦和耦合
  • 原文地址:https://www.cnblogs.com/tallme/p/11369782.html
Copyright © 2011-2022 走看看