Advertisement
Guest User

unit_test

a guest
Dec 6th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. import os
  2. import tempfile
  3.  
  4. import pytest
  5.  
  6. from flaskr import flaskr
  7.  
  8. @pytest.fixture
  9. def client():
  10.     db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
  11.     flaskr.app.config['TESTING'] = True
  12.     client = flaskr.app.test_client()
  13.  
  14.     with flaskr.app.app_context():
  15.         flaskr.init_db()
  16.  
  17.     yield client
  18.  
  19.     os.close(db_fd)
  20.     os.unlink(flaskr.app.config['DATABASE'])
  21.  
  22. def login(client, username, password):
  23.     return client.post('/login', data=dict(
  24.         username=username,
  25.         password=password
  26.     ), follow_redirects=True)
  27.  
  28. def logout(client):
  29.     return client.get('/logout', follow_redirects=True)
  30.  
  31. def test_login_logout(client):
  32.     rv = login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'])
  33.     assert b'You are logged in!' in rv.data
  34.  
  35.     rv = logout(client)
  36.     assert b'You are successfully logged out!' in rv.data
  37.  
  38.     rv = login(client, flaskr.app.config['USERNAME'] + 'x', flaskr.app.config['PASSWORD'])
  39.     assert b'Login Unsuccessful. Please check email and password' in rv.data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement