gpt4 book ai didi

python - 单独启动时的测试工作方式与使用 unittest 时的工作方式不同

转载 作者:行者123 更新时间:2023-12-01 05:28:58 25 4
gpt4 key购买 nike

我想测试我的应用程序

我有这两个文件(不是我真正的应用程序,但这提出了同样的问题)。

文件1:testServer.py

import SocketServer
import SimpleHTTPServer

class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

def do_GET(self):
f = self.wfile
self.send_response(200)
self.end_headers()
f.write("ok")

def runServer(port):
Handler = ServerHandler

httpd = SocketServer.TCPServer(("", port), Handler)

httpd.serve_forever()

if __name__ == "__main__":
runServer(8000)

文件2:test_testServer.py

import testServer
from multiprocessing import Process
import httplib
import unittest


class Test(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.serverProcess = Process(target = testServer.runServer, args = (8000,))
cls.serverProcess.daemon = True
cls.serverProcess.start()

@classmethod
def tearDownClass(cls):
cls.serverProcess.terminate()


def test_Server(self):
conn = httplib.HTTPConnection("localhost", 8000)
conn.request("GET", "")
response = conn.getresponse().read()
self.assertTrue(response, "ok")

if __name__ == "__main__":
unittest.main()

所以我的目标是启动我在进程中编码的服务器,然后测试它对不同请求的答复。

当我单独启动测试时,它工作得很好:

 C:\Users\r.bunel\workspace\Sandbox>python test_testServer.py
127.0.0.1 - - [23/Dec/2013 09:23:50] "GET / HTTP/1.1" 200 -

----------------------------------------------------------------------
Ran 1 test in 1.009s

OK

但是如果我通过使用unittest的discover将它作为我所有测试套件的一部分启动,它就不起作用:

C:\Users\r.bunel\workspace\Sandbox>python -m unittest discover
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python27\lib\multiprocessing\forking.py", line 380, in main
prepare(preparation_data)
File "C:\Python27\lib\multiprocessing\forking.py", line 488, in prepare
assert main_name not in sys.modules, main_name
AssertionError: __main__
E
======================================================================
ERROR: test_Server (test_testServer.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\r.bunel\workspace\Sandbox\test_testServer.py", line 22, in test_Server
conn.request("GET", "")
File "C:\Python27\lib\httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 1007, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 969, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 829, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 791, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 772, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
error: [Errno 10061] Aucune connexion nÆa pu Ûtre Útablie car lÆordinateur cible lÆa expressÚment refusÚe

----------------------------------------------------------------------
Ran 1 test in 2.009s

FAILED (errors=1)

所以,我的问题是,unittest 是否有这样做的理由,以及我有什么选择可以让这些测试在 unittest 内部工作?

最佳答案

也许单元测试正在尝试在第二个线程中正确启动之前连接到服务器?尝试在 setupClass(cls) 中的 cls.serverProcess.start() 之后添加 time.sleep(1)

@classmethod
def setUpClass(cls):
cls.serverProcess = Process(target = testServer.runServer, args = (8000,))
cls.serverProcess.daemon = True
cls.serverProcess.start()
time.sleep(1)

关于python - 单独启动时的测试工作方式与使用 unittest 时的工作方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com