繁体   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