我编写了一个python脚本,它接受命令行参数并播放一轮Tic Tac Toe.
运行它看起来像这样......
运行ttt o#xo ## x ## x 0 1
如果移动是合法的,那么它将打印新的电路板布局以及是否有人赢得了比赛
我必须使用unittest为它编写测试.我不知道如何使用各种命令行参数测试整个脚本,我看到的所有示例似乎只是测试脚本中的各个函数.脚本也用于argparse解析参数
谢谢!
我有这个代码在Python 2.5中运行良好,但在2.7中没有:
import sys
import traceback
try:
from io import StringIO
except:
from StringIO import StringIO
def CaptureExec(stmt):
oldio = (sys.stdin, sys.stdout, sys.stderr)
sio = StringIO()
sys.stdout = sys.stderr = sio
try:
exec(stmt, globals(), globals())
out = sio.getvalue()
except Exception, e:
out = str(e) + "\n" + traceback.format_exc()
sys.stdin, sys.stdout, sys.stderr = oldio
return out
print "%s" % CaptureExec("""
import random
print "hello world"
""")
Run Code Online (Sandbox Code Playgroud)
我得到:
string argument expected, got 'str' Traceback (most recent call last): File "D:\3.py", line 13, …