introdução a testes com django (2013)

23
INTRODUÇÃO A TESTES INTRODUÇÃO A TESTES COM COM DJANGO DJANGO Igor Leroy

Upload: igor-leroy

Post on 16-Jul-2015

174 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Introdução a testes com Django (2013)

INTRODUÇÃO A TESTESINTRODUÇÃO A TESTESCOM COM DJANGODJANGO

Igor Leroy

Page 2: Introdução a testes com Django (2013)

POR QUE?POR QUE?

PRA QUE? CONFIO NO MEU CÓDIGO, EU GARANTO!PRA QUE? CONFIO NO MEU CÓDIGO, EU GARANTO!RETRABALHO, RETRABALHO E CAÇA AOS BUGS.RETRABALHO, RETRABALHO E CAÇA AOS BUGS.NAAH... CHEGA DISSO.NAAH... CHEGA DISSO.TESTES DEVEM SER OBRIGATÓRIOS!TESTES DEVEM SER OBRIGATÓRIOS!

Page 3: Introdução a testes com Django (2013)

CULTURACULTURA

CÓDIGO CÓDIGO SIMPLESSIMPLES == FÁCIL DE TESTAR == FÁCIL DE TESTARCÓDIGO CÓDIGO MODULARIZADOMODULARIZADO == FÁCIL DE TESTAR == FÁCIL DE TESTARMODULARIZAÇÃO MODULARIZAÇÃO =! =! ABSTRAÇÃOABSTRAÇÃO

Page 4: Introdução a testes com Django (2013)

DJANGO 1.4, 1.5DJANGO 1.4, 1.5

project/myapp/tests.pyproject/myapp/tests.py

./manage.py test myapp./manage.py test myapp

Page 5: Introdução a testes com Django (2013)

TESTES UNITÁRIOSTESTES UNITÁRIOS

from django.test import TestCasefrom myapp.models import Animal

class AnimalTestCase(TestCase): def setUp(self): Animal.objects.create(name="lion", sound="roar") Animal.objects.create(name="cat", sound="meow")

def test_animals_can_speak(self): """Animals that can speak are correctly identified""" lion = Animal.objects.get(name="lion") cat = Animal.objects.get(name="cat") self.assertEqual(lion.speak(), 'The lion says "roar"') self.assertEqual(cat.speak(), 'The cat says "meow"')

Page 6: Introdução a testes com Django (2013)

TESTES FUNCIONAISTESTES FUNCIONAIS

import unittestfrom django.test import Client

class SimpleTest(unittest.TestCase): def setUp(self): self.client = Client()

def test_details(self): response = self.client.get('/customer/details/')

self.assertEqual(response.status_code, 200)

self.assertEqual(len(response.context['customers']), 5)

Page 7: Introdução a testes com Django (2013)

TESTS.PYTESTS.PY

UM ÚNICO MODULO DE TESTES É UMA UM ÚNICO MODULO DE TESTES É UMA MÁMÁ IDÉIA. IDÉIA.TESTES TAMBÉM SÃO CÓDIGOS, TESTES TAMBÉM SÃO CÓDIGOS, PRECISAMPRECISAM SER SERMODULARIZADOS.MODULARIZADOS.BEM ESCRITO E FÁCILBEM ESCRITO E FÁCIL DE ENTENDER. DE ENTENDER.

Page 8: Introdução a testes com Django (2013)

TEST RUNNERTEST RUNNER

DJANGO NOSE

https://github.com/jbalogh/django-nose

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

O NOSE possue mais coisas legais!

Page 9: Introdução a testes com Django (2013)

TEST ENVIRONMENTTEST ENVIRONMENT

createdb -U postgres_user -h localhost test_db

python manage.py syncdb --settings myproject.test_settingspython manage.py migrate --settings myproject.test_settings

os.environ['REUSE_DB'] = "1"

ou

REUSE_DB=1 python manage.py test

Page 10: Introdução a testes com Django (2013)

FIXTURESFIXTURES='(

{ "pk": 4, "model": "auth.user", "fields": { "username": "manager", "first_name": "", "last_name": "", "is_active": true, "is_superuser": false, "is_staff": false, "last_login": "2012-02-06 15:06:44",

Page 11: Introdução a testes com Django (2013)

FACTORY GIRL

FACTORY BOY

Page 12: Introdução a testes com Django (2013)

MODEL MOMMY!MODEL MOMMY!https://github.com/vandersonmota/model_mommy

Dados em memória.Se torna opcional salvar dados no DB.Rápido!

Page 13: Introdução a testes com Django (2013)
Page 14: Introdução a testes com Django (2013)

from model_mommy import mommyfrom family.models import Kid

kid = mommy.make(Kid)

Page 15: Introdução a testes com Django (2013)

MOMMY_MODELS.PYMOMMY_MODELS.PY

from model_mommy.recipe import Recipe, foreign_keyfrom family.models import Person, Dog

person = Recipe(Person, name = 'John Doe', nickname = 'joe', age = 18, birthday = date.today(), appointment = datetime.now())

dog = Recipe(Dog, breed = 'Pug', owner = foreign_key(person))

Page 16: Introdução a testes com Django (2013)

E AGORA?E AGORA?

Page 17: Introdução a testes com Django (2013)

.....myapp/

...................tests/

.............................test_models.py

.............................test_views.py

.............................test_urls.py

.............................test_forms.py

.............................test_features.py

Page 18: Introdução a testes com Django (2013)

BDD!BDD!

Client() é uma boa idéia?Testar somente nosso código python não faz nossosistema 100% livre de bugs.Precisamos testar as funcionalidades.

Page 19: Introdução a testes com Django (2013)

CAPYBARACAPYBARA

Page 20: Introdução a testes com Django (2013)

SPLINTERSPLINTER!!http://splinter.cobrateam.info

Page 21: Introdução a testes com Django (2013)
Page 22: Introdução a testes com Django (2013)

from splinter import Browser

browser = Browser()browser.visit('http://google.com')browser.fill('q', 'splinter - python acceptance testing for web applications')browser.find_by_name('btnG').click()

if browser.is_text_present('splinter.cobrateam.info'): print "Yes, the official website was found!"else: print "No, it wasn't found... We need to improve our SEO techniques"

browser.quit()

Page 23: Introdução a testes com Django (2013)

Obrigado!

@lerruagithub.com/lerrua