簡體   English   中英

Falcon, wsgiref:單元測試用例

[英]Falcon, wsgiref : Unit test cases

我有以下代碼:

master.py    

def create():
    master = falcon.API(middleware=Auth())
    msg = Message()
    master.add_route('/message', msg)


master = create()

if __name__ == '__main__':
    httpd = simple_server.make_server("127.0.0.1", 8989, master)
    process = Thread(target=httpd.serve_forever, name="master_process")
    process.start()

    #Some logic happens here
    s_process = Thread(target=httpd.shutdown(), name="shut_process")
    s_process.start()
    s.join()

我嘗試為以下內容創建以下測試用例:

from falcon import testing
from master import create

@pytest.fixture(scope='module')
def client():
   return testing.TestClient(create())

def test_post_message(client):
   result = client.simulate_post('/message', headers={'token': "ubxuybcwe"}, body='{"message": "I'm here!"}') --> This line throws the error
   assert result.status_code == 200

我嘗試運行上述但得到以下錯誤:

TypeError: 'NoneType' object is not callable

我實際上無法弄清楚我應該如何 go 為此編寫測試用例。

根據@hoefling 所說,以下修復了它:

master.py    

def create():
     master = falcon.API(middleware=Auth())
     msg = Message()
     master.add_route('/message', msg)
     return master


master = create()


if __name__ == '__main__':
    httpd = simple_server.make_server("127.0.0.1", 8989, master)
    process = Thread(target=httpd.serve_forever, name="master_process")
    process.start()

    #Some logic happens here
    s_process = Thread(target=httpd.shutdown(), name="shut_process")
    s_process.start()
    s.join()

然后測試用例起作用:

from falcon import testing
from master import create

@pytest.fixture(scope='module')
def client():
    return testing.TestClient(create())

def test_post_message(client):
    result = client.simulate_post('/message', headers={'token': "ubxuybcwe"}, 
    body='{"message": "I'm here!"}') 
    assert result.status_code == 200

非常感謝@hoefling!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM