Creating our test fixture

  • Import necessary Libraries
  • Create a temporary database
  • Create method to teadown the database

In [1]:
import os
import minitwit
import tempfile
import pytest
@pytest.fixture
def client(request):
    db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()
    client = minitwit.app.test_client()
    with minitwit.app.app_context():
        minitwit.init_db()

    def teardown():
        """Get rid of the database again after each test."""
        os.close(db_fd)
        os.unlink(minitwit.app.config['DATABASE'])
    request.addfinalizer(teardown)
    return client

Create our register, login, and logout helper functions

  • Calls our API using HTTP with the correct paths and parameters/data

In [2]:
def register(client, username, password, password2=None, email=None):
    """Helper function to register a user"""
    if password2 is None:
        password2 = password
    if email is None:
        email = username + '@example.com'
    return client.post('/register', data={
        'username':     username,
        'password':     password,
        'password2':    password2,
        'email':        email,
    }, follow_redirects=True)


def login(client, username, password):
    """Helper function to login"""
    return client.post('/login', data={
        'username': username,
        'password': password
    }, follow_redirects=True)


def register_and_login(client, username, password):
    """Registers and logs in in one go"""
    register(client, username, password)
    return login(client, username, password)


def logout(client):
    """Helper function to logout"""
    return client.get('/logout', follow_redirects=True)

Create a test for registering a new user

  • Checks return value from the service on success
  • Checks that duplicate users cannot be created
  • Checks that the username and password cannot be blank
  • Checks that password and password confirmation are equivalent

In [3]:
def test_register(client):
    """Make sure registering works"""
    rv = register(client, 'user1', 'default')
    assert b'You were successfully registered ' \
           b'and can login now' in rv.data
    rv = register(client, 'user1', 'default')
    assert b'The username is already taken' in rv.data
    rv = register(client, '', 'default')
    assert b'You have to enter a username' in rv.data
    rv = register(client, 'meh', '')
    assert b'You have to enter a password' in rv.data
    rv = register(client, 'meh', 'x', 'y')
    assert b'The two passwords do not match' in rv.data
    rv = register(client, 'meh', 'foo', email='broken')
    assert b'You have to enter a valid email address' in rv.data

In [6]:
pytest.main('-k register')


============================================================ test session starts ============================================================
platform darwin -- Python 2.7.6 -- py-1.4.26 -- pytest-2.6.4
collected 4 items

test_minitwit.py .

==================================================== 3 tests deselected by '-kregister' =====================================================
================================================== 1 passed, 3 deselected in 0.22 seconds ===================================================
Out[6]:
0

Create a test for logging in and logging out

  • Checks basic success case
  • Checks that passwords are validated properly
  • Checks that usernames are validated properly

In [5]:
def test_login_logout(client):
    """Make sure logging in and logging out works"""
    rv = register_and_login(client, 'user1', 'default')
    assert b'You were logged in' in rv.data
    rv = logout(client)
    assert b'You were logged out' in rv.data
    rv = login(client, 'user1', 'wrongpassword')
    assert b'Invalid password' in rv.data
    rv = login(client, 'user2', 'wrongpassword')
    assert b'Invalid username' in rv.data

In [5]:
pytest.main('-k login_logout')


============================================================ test session starts ============================================================
platform darwin -- Python 2.7.6 -- py-1.4.26 -- pytest-2.6.4
collected 4 items

test_minitwit.py .

================================================== 3 tests deselected by '-klogin_logout' ===================================================
================================================== 1 passed, 3 deselected in 0.31 seconds ===================================================
Out[5]:
0

In [ ]: