SlideShare a Scribd company logo
How you can write awesome code
if you have automated testing
Loek van Gent
Feb 3rd
2015
Loek van Gent #afk
Hallo! Wa tokelwap! Tjike!
PHP (Zend)many, many years
Django/Python
3 years
Who are you?
Ah, this is about me too?
Yeah why?
Why write tests?
➔
Documentation?
➔
Cross-browser compatibility?
➔
Doing small tweaks?
➔
Getting all the pixels right?
➔
Writing tests?
➔
…
Come on I'm a coder....
What’s the MOST fun in coding?
Exactly!!!
Writing awesome code!
What’s the MOST fun in coding?
What’s the LEAST fun in coding?
➔
Testing the entire project on every update?
➔
Fixing bugs?
➔
Crashing the live server?
I can see where this is going...
Bugs?
Bugs are nasty!
Write tests!
10
Automated tests
Unit tests Individual methods
Integration tests Grouped modules
Functional tests Full stack / Front-end
Some manual & user tests
Acceptance test Accept new features, Go/No-Go to live
Stress test Test server set-up (many requests)
Usability test Test UX
A/B testing Randomly present user option A or B. Test results.
...
Unit tests
Unit tests: An example
How can this possibly go wrong?
def early_bird_amount(amount):
amount = 0.8 * amount
return amount
100 → 80
Great update guys!
def early_bird_amount(amount):
-- amount = 0.8 * amount
++ amount = 0.6 * amount # Nice early bird deal
return amount
def early_bird_amount(amount):
-- amount = 0.8 * amount
++ amount = 0.6 * amount # 60% of standard
return amount
100 → 60
100 → 60
This should not go live.
def early_bird_amount(amount):
amount = 0.6 * amount # Nice early bird deal
amount = 0.6 * amount # 60% of standard
return amount
OOPS!
100 → 36
We should have tested that!
Ok, ok, I get it now.
Let's go!
from .tickets import early_bird_amount
from unittests import TestCase
class TestAmounts(TestCase):
def test_early_bird_amount(self):
calculated_amount = early_bird_amount(100)
self.assertEqual(calculated_amount, 60)
Is it really THAT easy?!
Assert
This is useful stuff!
assertEqual
assertNotEqual
assertGreater
assertIsNone
assertIsInstance
assertRaises
...
Unit test
Test only one “unit”
User model: get_name()
class User(models.Model):
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=40)
def get_name(self):
return “ ”.join(self.first_name,
self.last_name)
Let's test it.
class TestCase(unittest.TestCase):
def test_get_name(self):
instance = User.objects.create(
first_name="Django",
last_name="Reinhardt")
self.assertEqual(instance.get_name(),
"Django Reinhardt")
Done! This “unit” is tested, right?
Mock.
Nice! I'm loving this Mock stuff already!
class TestCase(unittest.TestCase):
def test_get_name(self):
mock_user = mock.Mock(spec=User)
mock_user.first_name = "Django"
mock_user.last_name = "Reinhardt"
self.assertEqual(User.get_name(mock_user),
"Django Reinhardt")
Functional tests
from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import
WebDriver
➔ Runs server localhost
➔ Starts Firefox (headless)
➔ Find html elements
➔ Send clicks
➔ Fill forms
➔ Assert elements, url, text
➔ much more
<li id=”reinhardt”>
<span class=”time”>16.15<span>
<span class=”speaker”>Django Reinhardt</span>
<em>Introduction to automated testing</em>
</li>
driver.get(“http://python-namibia.org/programme/”)
presentation = driver.find_element_by_id('reinhardt')
speaker =
presentation.find_element_by_class('speaker')
self.assertEqual(speaker.text, “Django Reinhardt”)
presentation.assertTextPresent(“automated testing”)
Live demo!
Automated Testing in Django
Automated Cross-browser testing
SauceLabs, BrowserStack,
TestingBot
Automated Testing in Django
Continuous Integration
Travis, Shippable, Jenkins
Automated Testing in Django
Automated Testing in Django
So, which test to use?
Well it's really up to you
Vital parts of your application
➔
payments
➔
permissions
➔
sensitive user data
Unit or integration tests
Essential user flows
➔
sign up / log in
➔
all “happy” flows
➔
donations, project registration
Functional tests
Questions?
https://github.com/gannetson
Workshop
➔
Django Testing Tutorial
➔
Bring you own project (Django?) and start writing tests.
➔
Write functional tests for a (live) website.
➔
Hookup Travis CI to you Github repo.
➔
Try out SauceLabs for manual cross browser testing.
➔
Anything...
Test Driven Development (TDD)
➔
Write tests for functionalities first, then code
➔
Write a test to catch a bug you find, then fix it.

More Related Content

What's hot (16)

PPT
20111018 boost and gtest
Will Shen
 
KEY
Unit Test Your Database
David Wheeler
 
PDF
Test Driven Development With Python
Siddhi
 
PDF
Py.test
soasme
 
PDF
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
PPTX
PHPUnit: from zero to hero
Jeremy Cook
 
PPT
Test Driven Development with PHPUnit
Mindfire Solutions
 
ODP
Testes pythonicos com pytest
viniciusban
 
PPT
How to test models using php unit testing framework?
satejsahu
 
PPT
Google mock for dummies
Harry Potter
 
PPTX
Unit Testing Presentation
nicobn
 
PPT
Advanced PHPUnit Testing
Mike Lively
 
PPTX
Laravel Unit Testing
Dr. Syed Hassan Amin
 
PDF
Keep your repo clean
Hector Canto
 
PDF
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
20111018 boost and gtest
Will Shen
 
Unit Test Your Database
David Wheeler
 
Test Driven Development With Python
Siddhi
 
Py.test
soasme
 
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
PHPUnit: from zero to hero
Jeremy Cook
 
Test Driven Development with PHPUnit
Mindfire Solutions
 
Testes pythonicos com pytest
viniciusban
 
How to test models using php unit testing framework?
satejsahu
 
Google mock for dummies
Harry Potter
 
Unit Testing Presentation
nicobn
 
Advanced PHPUnit Testing
Mike Lively
 
Laravel Unit Testing
Dr. Syed Hassan Amin
 
Keep your repo clean
Hector Canto
 
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 

Similar to Automated Testing in Django (20)

PDF
Testing Django Applications
Honza Král
 
PDF
Token Testing Slides
ericholscher
 
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
PPT
Test Driven
Alex Chaffee
 
PPT
Testing And Drupal
Peter Arato
 
PDF
DSR Testing (Part 1)
Steve Upton
 
PDF
Unit testing - A&BP CC
JWORKS powered by Ordina
 
PPTX
Unit tests & TDD
Dror Helper
 
PPTX
Testing in Python: doctest and unittest
Fariz Darari
 
PPTX
TDD Best Practices
Attila Bertók
 
PPTX
Tdd pecha kucha_v2
Paul Boos
 
PDF
Das Frontend richtig Testen – mit Jest @Developer Week 2018
Holger Grosse-Plankermann
 
PDF
Jest: Frontend Testing leicht gemacht @EnterJS2018
Holger Grosse-Plankermann
 
PPTX
An Introduction to unit testing
Steven Casey
 
KEY
Django’s nasal passage
Erik Rose
 
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove
 
PPTX
Testing in Python: doctest and unittest (Updated)
Fariz Darari
 
PDF
TDD super mondays-june-2014
Alex Kavanagh
 
PDF
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Holger Grosse-Plankermann
 
PPT
Automated Unit Testing
Mike Lively
 
Testing Django Applications
Honza Král
 
Token Testing Slides
ericholscher
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
Test Driven
Alex Chaffee
 
Testing And Drupal
Peter Arato
 
DSR Testing (Part 1)
Steve Upton
 
Unit testing - A&BP CC
JWORKS powered by Ordina
 
Unit tests & TDD
Dror Helper
 
Testing in Python: doctest and unittest
Fariz Darari
 
TDD Best Practices
Attila Bertók
 
Tdd pecha kucha_v2
Paul Boos
 
Das Frontend richtig Testen – mit Jest @Developer Week 2018
Holger Grosse-Plankermann
 
Jest: Frontend Testing leicht gemacht @EnterJS2018
Holger Grosse-Plankermann
 
An Introduction to unit testing
Steven Casey
 
Django’s nasal passage
Erik Rose
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove
 
Testing in Python: doctest and unittest (Updated)
Fariz Darari
 
TDD super mondays-june-2014
Alex Kavanagh
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Holger Grosse-Plankermann
 
Automated Unit Testing
Mike Lively
 
Ad

Recently uploaded (20)

PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PPTX
unit 2_2 copy right fdrgfdgfai and sm.pptx
nepmithibai2024
 
PPTX
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
PPTX
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
PDF
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
PPT
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
PDF
Web Hosting for Shopify WooCommerce etc.
Harry_Phoneix Harry_Phoneix
 
PPTX
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
PPT
introductio to computers by arthur janry
RamananMuthukrishnan
 
PDF
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
PPTX
英国假毕业证诺森比亚大学成绩单GPA修改UNN学生卡网上可查学历成绩单
Taqyea
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PPTX
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PPTX
本科硕士学历佛罗里达大学毕业证(UF毕业证书)24小时在线办理
Taqyea
 
PPTX
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
PDF
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
PPTX
internet básico presentacion es una red global
70965857
 
PPTX
Research Design - Report on seminar in thesis writing. PPTX
arvielobos1
 
PPTX
INTEGRATION OF ICT IN LEARNING AND INCORPORATIING TECHNOLOGY
kvshardwork1235
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
unit 2_2 copy right fdrgfdgfai and sm.pptx
nepmithibai2024
 
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
Web Hosting for Shopify WooCommerce etc.
Harry_Phoneix Harry_Phoneix
 
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
introductio to computers by arthur janry
RamananMuthukrishnan
 
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
英国假毕业证诺森比亚大学成绩单GPA修改UNN学生卡网上可查学历成绩单
Taqyea
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
本科硕士学历佛罗里达大学毕业证(UF毕业证书)24小时在线办理
Taqyea
 
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
internet básico presentacion es una red global
70965857
 
Research Design - Report on seminar in thesis writing. PPTX
arvielobos1
 
INTEGRATION OF ICT IN LEARNING AND INCORPORATIING TECHNOLOGY
kvshardwork1235
 
Ad

Automated Testing in Django

Editor's Notes

  • #3: Do-good crowdfunding Platform Django Rest Framework + Ember.JS
  • #4: Do-good crowdfunding Platform Django Rest Framework + Ember.JS
  • #5: Do-good crowdfunding Platform Django Rest Framework + Ember.JS