调试(测试-调试-优化)……待续

一、测试


二、调试

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)断点


三、优化

原文地址:https://www.cnblogs.com/llw1121/p/6675538.html