当我使用pytest在本地运行我的测试时,测试代码会通过所有测试,但如果我在travis上运行代码,它会抛出一个 keyerror:访问令牌 这是我的测试代码。
def register_user(self, first_name='Kali', last_name='sir3n',
email='user1234@gmail.com', password='testpassword'):
"""Implied registration . A helper method"""
user_data = {
'email': email,
'password': password,
'First Name': first_name,
'Last Name': last_name
}
return self.client().post('/api-1.0/auth/register', data=user_data)
def login_user(self, email='user1234@gmail.com', password='testpassword'):
"""Implied login. A helper method"""
user_data = {
'email': email,
'password': password,
}
return self.client().post('/api-1.0/auth/login', data=user_data)
def test_users_can_create_categories(self):
"""Test if users can create a recipe category(POST)"""
self.register_user()
result = self.login_user()
access_token = json.loads(result.data.decode())['Access token']
# it throws an error on the above basis saying that the access token
doesnot exist
res = self.client().post('/api-1.0/categories/', data={
"name": "Sweet pie",
"detail": "Made by mama"
},
headers=dict(Authorization="Bearer " + access_token)
)
self.assertEqual(res.status_code, 201)
self.assertIn('Sweet pie', str(res.data))
我已正确配置了数据库,因此不是问题 在本地运行命令确实给了我一个访问令牌 使用postman测试api也会返回访问令牌 任何建议将不胜感激