关于测试:
测试是用来证明错误的存在,尽管可能对许许多多的情况都是OK的,但是只要发现一处有问题就说明有问题。
1、黑盒测试
黑盒测试是指,构建黑盒测试时不需要查看要测试的代码。黑盒测试允许测试者和开发者来自不同人群。
2、白盒测试
白盒测试是指能够看待测试的代码及内部结构进行测试。不检查代码内部结构,就不可能知道哪种测试用例能提供新的信息。
异常和断言
捕捉异常可以使用try/except语句。
捕捉异常的语法如下:
try:
<语句> #运行别的代码
except <名字>:
<语句> #如果在try部份引发了'name'异常
except <名字>,<数据>:
<语句> #如果引发了'name'异常,获得附加的数据
else:
<语句> #如果没有异常发生
实例:
try:
fh = open("testfile", "w")
fh.write("这是一个测试文件,用于测试异常!!")
except IOError:
print "Error: 没有找到文件或读取文件失败"
else:
print "内容写入文件成功"
fh.close()
断言
断言assert 的作用:在开发一个程序时候,与其让它运行时崩溃,不如在它出现错误条件时就崩溃(返回错误)
assert的语法格式:
assert expression
In [17]: str_a = 'this is string'
In [18]: type(str_a)
Out[18]: str
In [19]: assert type(str_a) == str
In [20]: assert type(str_a) == int
Traceback (most recent call last):
File "", line 1, in
assert type(str_a) == int
AssertionError
试题错误(stackoverflow大法好):
Problem Set 4
在py3下需将inFile = open(WORDLIST_FILENAME, 'r',0) 更改为inFile = open(WORDLIST_FILENAME, 'r'),不然会报错。
L7 PROBLEM 4
关于测试套件,应该满足测试套件给出的条件应该能够测试所有函数提供的可能性。
def union(set1, set2):
"""
set1 and set2 are collections of objects, each of which might be empty.
Each set has no duplicates within itself, but there may be objects that
are in both sets. Objects are assumed to be of the same type.
This function returns one set containing all elements from
both input sets, but with no duplicates.
"""
if len(set1) == 0:
return set2
elif set1[0] in set2:
return union(set1[1:], set2)
else:
return set1[0] + union(set1[1:], set2)
测试集应为:
Test Suite D: union('','abc'), union('a','abc'), union('ab','abc'), union('d','abc')
期中考试:
quiz(期中考试中的quiz和编程测验中的试题有重复):
PROBLEM 4
Write a simple procedure, myLog(x, b), that computes the logarithm of a number x relative to a base b.
For example, if x = 16 and b = 2, then the result is 4 - because 24=16. If x = 15 and b = 3,
then the result is 2 - because 32 is the largest power of 3 less than 15.
解决方案之一为:
In [57]: def myLog(x, b):
...: if x == b:
...: return 1
...: elif x < b:
...: return 0
...: else:
...: # 体会这里的这个递归
...: return 1 + myLog (x / b, b)
PROBLEM 6
Write a recursive procedure, called laceStringsRecur(s1, s2), which also laces together two strings.
Your procedure should not use any explicit loop mechanism, such as a for or while loop.
We have provided a template of the code; your job is to insert a single line of code in each of the indicated places.
解决方案之一为:
In [60]: def laceStringsRecur(s1, s2):
...: """
...: s1 and s2 are strings.
...:
...: Returns a new str with elements of s1 and s2 interlaced,
...: beginning with s1. If strings are not of same length,
...: then the extra elements should appear at the end.
...: """
...: def helpLaceStrings(s1, s2, out):
...: if s1 == '':
...: return s2
...: if s2 == '':
...: return s1
...: else:
...: return s1[0] + s2[0] + helpLaceStrings(s1[1:], s2[1:], '')
...: return helpLaceStrings(s1, s2, '')
...:
In [61]: laceStringsRecur('prgv', 'rtgwnisohg')
Out[61]: 'prrtggvwnisohg'
PROBLEM 7
6a+9b+20c=n
解决方案之一为:
In [81]:
...: def McNuggets(n):
...: """
...: n is an int
...: Returns True if some integer combination of 6, 9 and 20 equals n
...: Otherwise returns False.
...: """
...: if n == 0:
...: return True
...: else:
...: for factor in [20, 9, 6]:
...: if n >= factor and McNuggets(n - factor):
...: return True
...: return False
...:
In [82]: McNuggets(9)
Out[82]: True
In [83]: McNuggets(15)
Out[83]: True
参考链接:
6.00.1X 计算机科学和PYTHON编程导论(自主模式)
How can I fix this “ValueError: can't have unbuffered text I/O” in python 3?
6.00.1x-intro-computer-science
edx-mitx-6.00.1x
edx-mitx-6.00/midTerm1_1.py