Skip to content

Instantly share code, notes, and snippets.

@alexandre
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save alexandre/6be3eb2dd6718b92aed7 to your computer and use it in GitHub Desktop.

Select an option

Save alexandre/6be3eb2dd6718b92aed7 to your computer and use it in GitHub Desktop.
um exemplo de como [atualmente] eu organizo os meus testes

Exemplo pensando em uma API simples para gerenciar usuários.

Eu tenho uma interface que recebe os requests (e.g. GET, POST) e chama uma função que executa a operação

Eu começo com um teste unitário pensando na função que executará X tarefa.

def test_create_user_invalid_age():
    assert create_user('Foo Jr', 'Test!') == {'error': 'Invalid name'}

def create_user(name, age):
    if not age.isdigit():
        return {'error': 'Invalid name'}
    else:
        ... #creating user
        return {'created': 'ID: 123123'}

Depois de finalizar a função, eu testo a integração dela na API...(e.g. um request HTTP)

def test_post_new_user():
    payload = {
        'age': '123',
        'name':'Foo Jr'
    }
    resp = client.post('/api/user', data=json.dumps(payload))

    assert resp.status_code == 201
    assert 'created' in json.loads(resp.data.decode('utf-8')).keys()

Testes passando? Git Commit/Push, CI -> Deploy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment