pytest-使用

时间:2019-11-04
本文章向大家介绍pytest-使用,主要包括pytest-使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 import pytest
"""
 使用pytest编写用例,必须遵守以下规则:
    (1)测试文件名必须以“test_”开头或者"_test"结尾(如:test_ab.py)
  (2)测试方法必须以“test_”开头。
  (3)测试类命名以"Test"开头。
"""

"""
@pytest.fixture(scope='')
    function:每个test都运行,默认是function的scope
    class:每个class的所有test只运行一次
    module:每个module的所有test只运行一次
    session:每个session只运行一次
"""

@pytest.fixture(scope='function')
def setup_function(request):
    def teardown_function():
        print("teardown_function called.")
    request.addfinalizer(teardown_function)  # 此内嵌函数做teardown工作
    print('setup_function called.')

@pytest.mark.website
def test_1(setup_function):
    print('Test_1 called.')

@pytest.fixture(scope='module')
def setup_module(request):
    def teardown_module():
        print("teardown_module called.")
    request.addfinalizer(teardown_module)
    print('setup_module called.')

def test_2(setup_module):
    print('Test_2 called.')

def test_3(setup_module):
    print('Test_3 called.')
    assert 2==1+1

if __name__ == '__main__':
    """
    Console参数介绍
        -v 用于显示每个测试函数的执行结果
        -q 只显示整体测试结果
        -s 用于显示测试函数中print()函数输出
        -x, --exitfirst, exit instantly on first error or failed test
        -h 帮助
    """
    """
    报告参数 _report.html   ./_report.html   ./report/_report.html    ../report/_report.html    #./当前文件夹,../上个文件夹
        --resultlog=./log.txt
        --junitxml=./log.xml
        --pastebin=all
        --html=./report.html 
    """

    #两者方法相同,执行该目录下所有的test文件
    #pytest.main()
    #pytest.main(["D:\codeBook\pyhton\shuzf_demo\other\pytest"])

    # pytest.main(['run_pytest.py'])
    # pytest.main(['-q', 'run_pytest.py'])         #多了版本信息
    # pytest.main(['-s', 'run_pytest.py'])         #以print信息显示
    # pytest.main(['-s', 'run_pytest.py', '--html=_report.html'])

原文地址:https://www.cnblogs.com/shuzf/p/11792745.html