Download - Introdução a testes com Django (2013)
![Page 1: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/1.jpg)
INTRODUÇÃO A TESTESINTRODUÇÃO A TESTESCOM COM DJANGODJANGO
Igor Leroy
![Page 2: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/2.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/3.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/4.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/5.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/6.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/7.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/8.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/9.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/10.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/11.jpg)
FACTORY GIRL
FACTORY BOY
![Page 12: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/12.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/13.jpg)
![Page 14: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/14.jpg)
from model_mommy import mommyfrom family.models import Kid
kid = mommy.make(Kid)
![Page 15: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/15.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/16.jpg)
E AGORA?E AGORA?
![Page 17: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/17.jpg)
.....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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/18.jpg)
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)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/19.jpg)
CAPYBARACAPYBARA
![Page 20: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/20.jpg)
SPLINTERSPLINTER!!http://splinter.cobrateam.info
![Page 21: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/21.jpg)
![Page 22: Introdução a testes com Django (2013)](https://reader034.vdocuments.mx/reader034/viewer/2022052316/55a77d911a28abb4668b47c4/html5/thumbnails/22.jpg)
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()