automated testing in django

Download Automated Testing in Django

If you can't read please download the document

Upload: loek-van-gent

Post on 12-Aug-2015

84 views

Category:

Internet


2 download

TRANSCRIPT

  1. 1. How you can write awesome code if you have automated testing Loek van Gent Feb 3rd 2015
  2. 2. Loek van Gent #afk Hallo! Wa tokelwap! Tjike!
  3. 3. PHP (Zend)many, many years Django/Python 3 years
  4. 4. Who are you? Ah, this is about me too?
  5. 5. Yeah why? Why write tests?
  6. 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. 7. Exactly!!! Writing awesome code! Whats the MOST fun in coding?
  8. 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. 9. Bugs? Bugs are nasty! Write tests!
  10. 10. 10
  11. 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. 12. Unit tests
  13. 13. Unit tests: An example How can this possibly go wrong? def early_bird_amount(amount): amount = 0.8 * amount return amount 100 80
  14. 14. 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
  15. 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. 16. We should have tested that! Ok, ok, I get it now.
  17. 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. 18. Assert This is useful stuff! assertEqual assertNotEqual assertGreater assertIsNone assertIsInstance assertRaises ...
  19. 19. Unit test Test only one unit
  20. 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. 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?
  22. 22. 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")
  23. 23. Functional tests
  24. 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
  25. 25.
  26. 16.15 Django Reinhardt Introduction to automated testing 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)
  27. 26. Live demo!
  28. 27. Automated Cross-browser testing SauceLabs, BrowserStack, TestingBot
  29. 28. Continuous Integration Travis, Shippable, Jenkins
  30. 29. So, which test to use?
  31. 30. Well it's really up to you
  32. 31. Vital parts of your application payments permissions sensitive user data Unit or integration tests
  33. 32. Essential user flows sign up / log in all happy flows donations, project registration Functional tests
  34. 33. Questions? https://github.com/gannetson
  35. 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...
  36. 35. Test Driven Development (TDD) Write tests for functionalities first, then code Write a test to catch a bug you find, then fix it.