1. How you can write awesome code if you have automated testing
Loek van Gent Feb 3rd 2015
2. Loek van Gent #afk Hallo! Wa tokelwap! Tjike!
3. PHP (Zend)many, many years Django/Python 3 years
4. Who are you? Ah, this is about me too?
5. Yeah why? Why write tests?
6. Documentation? Cross-browser compatibility? Doing small
tweaks? Getting all the pixels right? Writing tests? Come on I'm a
coder.... Whats the MOST fun in coding?
7. Exactly!!! Writing awesome code! Whats the MOST fun in
coding?
8. Whats 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...
9. Bugs? Bugs are nasty! Write tests!
10. 10
11. 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. ...
12. Unit tests
13. Unit tests: An example How can this possibly go wrong? def
early_bird_amount(amount): amount = 0.8 * amount return amount 100
80
15. 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
16. We should have tested that! Ok, ok, I get it now.
17. 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?!
18. Assert This is useful stuff! assertEqual assertNotEqual
assertGreater assertIsNone assertIsInstance assertRaises ...
19. Unit test Test only one unit
20. 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)
21. 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?
24. 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
31. Vital parts of your application payments permissions
sensitive user data Unit or integration tests
32. Essential user flows sign up / log in all happy flows
donations, project registration Functional tests
33. Questions? https://github.com/gannetson
34. 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...
35. Test Driven Development (TDD) Write tests for
functionalities first, then code Write a test to catch a bug you
find, then fix it.