首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Falcon,wsgiref :单元测试用例

Falcon,wsgiref :单元测试用例
EN

Stack Overflow用户
提问于 2019-10-25 23:01:02
回答 1查看 148关注 0票数 0

我有以下代码:

代码语言:javascript
复制
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()

我尝试为以下内容创建以下测试用例:

代码语言:javascript
复制
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

我尝试运行上面的代码,但得到以下错误:

代码语言:javascript
复制
TypeError: 'NoneType' object is not callable

实际上,我不知道该如何编写测试用例。

EN

回答 1

Stack Overflow用户

发布于 2019-10-26 13:19:17

根据@hoefling的说法,下面修复了它:

代码语言:javascript
复制
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()

然后测试用例就可以工作了:

代码语言:javascript
复制
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!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58561028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档