zoukankan      html  css  js  c++  java
  • 调试(测试-调试-优化)……待续

    一、测试


    二、调试

    1、抛出异常——针对可恢复的错误

    ① try-except语句                          :在调用该函数的代码中

    ② raise语句—raise Exception()    :在函数中

    2、取得反向跟踪的字符串——调用栈

    traceback模块

    >>> import traceback
    >>> try:
        raise Exception('This is the error message.')
    except:
        errorFile = open('errorInfo.txt', 'w')
        errorFile.write(traceback.format_exc())
        errorFile.close()
        print('The traceback info was written to errorInfo.txt .')
    
        
    Traceback (most recent call last):
      File "<pyshell#8>", line 2, in <module>
        raise Exception('This is the error message.')
    Exception: This is the error message.

    3、断言——针对开发,即程序员的错误

    1)assert语句,包括:

    ①assert关键字;②条件(即求值为True或者False的表达式);③逗号;④条件为False时显示的字符串;

    >>> DoorStatus = 'open'
    >>> assert DoorStatus == 'open','The doors need to be "open".'
    >>> DoorStatus = 'I am sorry,I can not do that.'
    >>> assert DoorStatus == 'open','The doors need to be "open".'
    
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        assert DoorStatus == 'open','The doors need to be "open".'
    AssertionError: The doors need to be "open".

    2)禁用断言

    传入-O选项

    4、日志——logging模块

     1)使用日志模块

    import logging
    logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')

    2)打印日志信息

    logging.debug()

    3)日志级别

    DEBUG logging.debug()
    INFO logging.info()
    WARNING logging.warning()
    ERROR logging.error()
    CRITICAL logging.critical()

    4)禁用日志

    logging.disable(传入的日志级别)

    5)将日志记录到文件

    import logging
    logging.basicConfig(filename='myProgromLog.txt', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')


    5、IDLE调试器

    1)Go,Step,Over,Out,Quit

    2)断点


    三、优化

  • 相关阅读:
    算法入门7:分支限界法
    算法入门5:贪心算法
    算法入门4:动态规划
    变量
    Java标识符
    Java中的关键字
    Groovy 配置环境变量
    Robot Framework学习笔记(一)------环境搭建
    关于谷歌浏览器(chrome)的一些好用的插件推荐
    关于UML方法学图中类之间的关系:依赖,泛化,关联
  • 原文地址:https://www.cnblogs.com/llw1121/p/6675538.html
Copyright © 2011-2022 走看看