1

我正在尝试在烧瓶中对我的应用程序进行单元测试,但是我对此有点卡住了。我想在我的应用程序路由中添加一个参数,但我不知道该怎么做。有谁能够帮我?

我现在在测试中设置了硬编码的空缺 id,但我将向数据库添加一个连接和一个查询。但我首先想让这个工作

这是路线:

@app.route('/add_application/<vacancy_id>')
def add_application(vacancy_id):
   return render_template(
        'addapplication.html')

这是我到目前为止的测试:

VACANCY_ID = '5f67bc3643f71774b981ebfc'

def test_addapplicationFromVacancy(self):
    tester = app.test_client(self)
    tester.post(
        '/login',
        data=dict(username=USERNAME_USER, password=SPW_TWO),
        follow_redirects=True
    )
    response = tester.get(
        '/add_application/',
        data={'vacancy_id': VACANCY_ID},
        content_type='html/text'
    )
    print()
    self.assertEqual(response.status_code, 200)
4

1 回答 1

2

我不知道你的整个申请,但我至少看到两件事可以帮助你:

  1. 您不应该self在创建客户端期间使用,因为这是unittest.TestCase类的对象,而不是 Flask 应用程序的对象。并且会自动通过。
    tester = app.test_client() 
    
  2. 您的端点使用 URL 参数,因此 get 请求应该或多或少像这样:
    tester.get(f"/add_application/{VACANCY_ID}", ...)
    
    *我在这里使用了 f-string,如果你没有使用 >=3.6 版本的 Python,请随意更改它
于 2020-10-10T12:46:55.327 回答