在Python中,测试函数的标准输出(In Python, testing the standard output of a function)

编程入门 行业动态 更新时间:2024-10-26 10:37:52
在Python中,测试函数的标准输出(In Python, testing the standard output of a function)

我一直在尝试编写一个测试(使用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()

因此,我的测试需要测试这样一个事实:当这个函数独立运行时(不传递任何参数),它会输出'usage:./ pyPeerChat [IP ADDRESS of pc] / [0(如果网络不知道)这将假设这个对等体将成为新网络的开始)]'。

到目前为止,我目前一直试图实施的测试是:

import myModuleChat from io import StringIO import unittest from mock import patch def main(): Testmain.test_main_prints_without_ARGs() class TestMain(unittest.TestCase): def test_main_prints_without_ARGs(self): expected_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)]' with patch('sys.stdout', new=StringIO()) as fake_out: pyPeerChat.main() self.assertEqual(fake_out.getvalue(), expected_print) if __name__ == '__main__': test_program = unittest.main(verbosity=0, buffer=False, exit=False)

但是,我一直无法通过成功通过此测试。 测试失败,我收到错误。 以下是测试的整个输出,错误如下:

====================================================================== 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)

我不确定这个错误意味着什么,因为代码工作正常。 有没有更简单的方法来编写我需要的测试,或者一种方法来修复我的测试?

I've been trying to write a test (using unittest) to test the output of a function. The function is a follows:

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()

So, my test would need test the fact that, when this function runs on its own (not being passed any arguments), it prints '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)]'.

So far, the test I've currently been trying to implement has been:

import myModuleChat from io import StringIO import unittest from mock import patch def main(): Testmain.test_main_prints_without_ARGs() class TestMain(unittest.TestCase): def test_main_prints_without_ARGs(self): expected_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)]' with patch('sys.stdout', new=StringIO()) as fake_out: pyPeerChat.main() self.assertEqual(fake_out.getvalue(), expected_print) if __name__ == '__main__': test_program = unittest.main(verbosity=0, buffer=False, exit=False)

However, I've been unable to get this test to pass sucessfully. The test fails, and I get an error. Below is the entire output of the test, with the error:

====================================================================== 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)

I'm not at all sure what this error means because the code works fine. Is there an easier way to write the test I need, or a way to fix my test?

最满意答案

好的,经过一番搜索,我找到了如何将stdout从我的函数绑定到变量。 然后,使用assertEqual(),我能够将它与'expected'字符串进行比较:

from pyPeerChat import main from StringIO import StringIO import unittest class TestFoo(unittest.TestCase): def test_output_without_args(self): out = StringIO() main(out=out) output = out.getvalue().strip() expected = '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)]' self.assertEqual(output, expected) if __name__ == '__main__': unittest.main() #'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)]'

这给了我一个成功通过的测试。

Okay, so after a bit more searching, I worked out how to bind the stdout from my function to a variable. Then, using assertEqual(), I was able to compare it to an 'expected' string:

from pyPeerChat import main from StringIO import StringIO import unittest class TestFoo(unittest.TestCase): def test_output_without_args(self): out = StringIO() main(out=out) output = out.getvalue().strip() expected = '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)]' self.assertEqual(output, expected) if __name__ == '__main__': unittest.main() #'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)]'

This gives me a successfully passed test.

更多推荐

本文发布于:2023-08-08 00:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   测试   标准   Python   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!