2017-05-09 7 views
0

私はJavaのバックグラウンドから来ており、Pythonでは全く新しいものです。Tornadoサーバーの統合テスト

が、私はそのようなWebサーバーを起動するPythonプログラムhello.pyがあるとします。

import tornado.web 
import tornado.httpserver 
import tornado.ioloop 

def start_server(port, handler): 
    app = tornado.web.Application([(r"/", handler), ]) 
    server = tornado.httpserver.HTTPServer(app) 
    server.bind(port) 
    server.start(0) # autodetect number of cores and fork a process for each 
    tornado.ioloop.IOLoop.instance().start() 

class MyHandler(tornado.web.RequestHandler): 

    def get(self): 
     self.write("Hello!") 

start_server(5000, MyHandler) 

今、私はPythonでこのサーバーの統合テストを書きたいと思います。テストする必要があります:応答== "Hello!"

  • がサーバー
  • を停止した場合、URL http://localhost:5000/helloに同期し

  • チェックをHTTPのGETリクエストを送信し、サーバ
  • を開始する

    • 実行hello.pyどのようにそのようなテストを書くことを提案しますか?

  • +1

    この例のようなものが必要でしょうか? http://www.tornadoweb.org/en/stable/testing.html#tornado.testing.AsyncHTTPTestCase – kepler

    答えて

    1

    tornadotestディレクトリを参照することをお勧めします。それにはtornadoのすべてのテストが含まれており、pythonまたはnosetestsで起動できます。

    httpserver_test.pyをご覧ください。

    +0

    さて、そこにはたくさんのものがありますが、私が必要とするテストは見つけられませんでした。何かが欠けています。私は非常に単純なものが必要です。サーバーを別のスレッドで実行し、HTTP経由でサーバーと通信するPythonスクリプトを作成したいのですが、それはPythonで実行可能ですか? – Michael