zoukankan      html  css  js  c++  java
  • pytest以函数形式形成测试用例

    #coding=utf-8
    from __future__ import print_function
    
    #开始执行该文件时,该函数执行
    def setup_module(module):
        print('
    setup_module()')
    
    #结束执行该文件时,该函数执行
    def teardown_module(module):
        print('teardown_module()')
    
    
    #单元测试函数执行之前该函数执行
    def setup_function(function):
        print('
    setup_function()')
    #单元测试函数执行之后该函数执行
    def teardown_function(function):
        print('
    teardown_function()')
    
    #case1
    def test_1():
        print('-  test_1()')
    
    #case2
    def test_2():
        print('-  test_2()')

    输出

    bogon:test macname$ pytest test.py -s
    ============================= test session starts ==============================
    platform darwin -- Python 3.6.3, pytest-5.1.0, py-1.8.0, pluggy-0.12.0
    rootdir: /Users/macname/Desktop/test
    collected 2 items                                                              
    
    test.py 
    setup_module()
    
    setup_function()
    -  test_1()
    .
    teardown_function()
    
    setup_function()
    -  test_2()
    .
    teardown_function()
    teardown_module()
    
    
    ============================== 2 passed in 0.03s ===============================

    另外一种方式

    bogon:test macname$ pytest test.py::test_1
    ============================================ test session starts =============================================
    platform darwin -- Python 3.6.3, pytest-5.1.0, py-1.8.0, pluggy-0.12.0
    rootdir: /Users/macname/Desktop/test
    collected 1 item                                                                                             
    
    test.py .                                                                                              [100%]
    
    ============================================= 1 passed in 0.01s ==============================================
    bogon:test macname$ pytest test.py::test_2
    ============================================ test session starts =============================================
    platform darwin -- Python 3.6.3, pytest-5.1.0, py-1.8.0, pluggy-0.12.0
    rootdir: /Users/macname/Desktop/test
    collected 1 item                                                                                             
    
    test.py .                                                                                              [100%]
    
    ============================================= 1 passed in 0.01s ==============================================
  • 相关阅读:
    撬动百亿VRAR产业,让VR们“造”起来
    带你熟悉鸿蒙轻内核Kconfig使用指南
    教你Python字符串的基本操作:拆分和连接
    从翻硬币游戏看敏捷开发
    求助,请教各位,如何牵头做一个项目
    Qt三方库开发技术:QXlsx介绍、编译和使用
    项目实战:Qt+ffmpeg摄像头检测工具
    OpenSSL 自述
    用故事说透HTTPS
    一起来看看大佬是怎样配置nginx虚拟主机
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11382974.html
Copyright © 2011-2022 走看看