我正在尝试在travisCI上运行pytest,并且在尝试发送电子邮件时测试失败
课程
class AdminChangeLocation(Resource):
"""Admin can change the destination of the parcel"""
@jwt_required
def put(self, parcelid):
payload = api.payload
if not payload:
return {'status': 'failed', 'message': 'please provide a json data'}, 400
if not all(key in payload for key in ['location']):
return {'status': 'failed', 'message': 'please provide the destination'}, 400
check_empty = CheckRequired(payload)
checked_empty = check_empty.check_data_payload()
if not checked_empty:
return {'status': 'failed', 'message': 'bad request no empty value allowed'}, 400
parcel = ParcelModel()
parcels = parcel.get_parcel(parcelid)
current_user = get_jwt_identity()
role = current_user['role']
if not role == 'admin':
return {'status': 'failed', 'message': 'Unauthorised. you are not allowed'}, 403
if not parcels:
return {'status': 'failed', 'message': 'parcel ID does not exist', "data": parcels}, 404
change = parcel.change_location(payload['location'], parcelid)
if not change:
return {'status': 'failed', 'message': 'There was an error processing data'}, 500
parcels['current_location'] = payload['location']
# get the user_id
user_id = parcels["user_id"]
# Get the user email
user = UserModel()
user_data = user.get_user_email(user_id)
user_email = user_data[1]
message = "Hey Customer the current location of your parcel " + parcelid + " is now in\n" \
"" + parcels['current_location']
email = SendMail()
email.send_mail(message, user_email, "Parcel update")
return {'status': 'success', 'message': 'Waiting for confirmation', "data": parcels}, 202
测试
"""This test case tets the parcel test cases"""
def test_admin_change_location(self):
"""Test API if it adds a parcel(POST)"""
data = {"email": "admin@gmail.com",
"password": "admin",
"role": "admin"}
data2 = {"email": "langatchirchir@gmail.com",
"password": "kevin12345",
"role": "user"}
location = {"location": "Nairobi"}
res = self.client().post("api/v2/auth/signup", json=self.admin)
res2 = self.client().post("api/v2/auth/login", json=data)
data = json.loads(res2.get_data(as_text=True))
token_admin = data['data']['token']
res = self.client().post("api/v2/auth/signup", json=self.user)
res2 = self.client().post("api/v2/auth/login", json=data2)
data = json.loads(res2.get_data(as_text=True))
token_user = data['data']['token']
res = self.client().post("api/v2/parcels", json=self.parcel, headers=dict(Authorization="Bearer " + token_user))
data = json.loads(res.get_data(as_text=True))
order_id = data['parcel']['order_no']
res = self.client().put("api/v2/parcels/" + order_id + "/presentLocation", json=location,
headers=dict(Authorization="Bearer " + token_admin))
self.assertEqual(res.status_code, 202)
self.assertIn("Nairobi", str(res.data))
Travis.yml
language: python python: - "3.6" install: - pip install -r requirements.txt - pip install pytest - pip install pytest-cov - pip install coveralls - pip install psycopg2-binary services: - postgresql before_script: - psql -c 'create database send_it_test;' -U postgres - export DB_TEST="dbname='send_it_test' host='localhost' port='5432' user='postgres' password=''" - export FLASK_ENV=testing # email variables have been set on travis .env script: - pytest --cov=app/ after_success: - coveralls
答案 0 :(得分:0)
我找到了解决方案。由于Travis阻止了所有SMTP端口,我使用了if函数来检查环境是否正在测试,从而绕过了测试 这是我的代码
class SendMail:
def __init__(self):
pass
def send_mail(self, message, to, subject):
# Gmail Sign In
if not os.getenv('FLASK_ENV') == 'testing':
gmail_sender = os.getenv("EMAIL")
gmail_passwd = os.getenv("EMAIL_PASS")
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)
BODY = '\r\n'.join(['To: %s' % to,
'From: %s' % gmail_sender,
'Subject: %s' % subject,
'', message])
server.sendmail(gmail_sender, [to], BODY)
res = "sent"
except:
res = "fail"
server.quit()
return res
return True