我一直在尝试编写一个测试(使用unittest)来测试函数的输出。
函数如下:def main():
arg_pressent = len(sys.argv)
if arg_pressent < 2:
print "usage: ./pyPeerChat [IP ADDRESS of pc] / [0 (if the network is not known. This will assume that this peer will be the start of a new network)]"
else:
IP = str(sys.argv[1])
connect.main(IP)
if __name__ == '__main__':
main()
所以,我的测试需要测试这样一个事实:当这个函数独立运行时(没有传递任何参数),它会打印出用法:./pyPeerChat[IP ADDRESS of pc]/[0(如果网络未知)。这将假定这个对等端将是一个新网络的开始。在
到目前为止,我正在尝试实施的测试是:
^{pr2}$
然而,我一直没能成功地通过这次考试。测试失败了,我得到了一个错误。以下是测试的整个输出,错误如下:======================================================================
ERROR: test_main_prints_without_ARGs (__main__.TestMain)
----------------------------------------------------------------------
Traceback (most recent call last):
File "testpyPeerChat.py", line 24, in test_main_prints_without_ARGs
pyPeerChat.main()
File "/home/peer-node/Testing/P2PChat/source/pyPeerChat.py", line 47, in main
print "usage: ./pyPeerChat [IP ADDRESS of Bootpeer] / [0 (if the network is not known. This will assume that this peer will be the start of a new network)]"
TypeError: unicode argument expected, got 'str'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
我完全不确定这个错误意味着什么,因为代码运行良好。有没有更简单的方法来编写我需要的测试,或者修复我的测试?在