为Web应用程序编写测试

时间:2019-02-03 09:43:01

标签: python testing pytest bottle

我在学习时正在编写一个Web应用程序,我编写了一个接收文件的功能。现在我想为此编写一个测试函数。我正在使用pytest。

在测试功能中,我将需要提交文件,我已经检查了requests,但是没有得到。请显示一些光线,以便我可以为以下给定功能创建测试功能。

@app.post('/check/<number>')
def file_upload(number):
    u_name = request.forms.get('username')  # accepting username
    time = datetime.datetime.now()
    # type(uploaded) == <class 'bytes'>
    uploaded = request.files.get('upload').file.read()  # uploaded outputs by user
    expected = questions[number].output
    expected = expected.strip()
    uploaded = uploaded.strip()
    ans = (uploaded == expected)
    usernames[u_name].append(Submission(question=number, time=time,
                                        output=uploaded, result=ans))
    if not ans:
        return 'Wrong answer'
    else:
        return 'Solved! Great Job!'

运行测试文件时,我希望200响应。

1 个答案:

答案 0 :(得分:0)

使用WebTest

from webtest import TestApp

test_app = TestApp(app)
resp = test_app.post('/check/123')  # by default, this fails if your app returns anything except HTTP 2XX

Stack Overflow中的其他一些示例:

Test bottle app without running bottle server

Unit testing Bottle app with WebTest