Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Guru Sorocaba - Testing in Django

Guru Sorocaba - Testing in Django

Online Meetup organised by Guru Sorocaba, Brazil

Kátia Nakamura

May 12, 2017
Tweet

More Decks by Kátia Nakamura

Other Decks in Programming

Transcript

  1. Kátia Nakamura From Mogi das Cruzes, SP, Brazil Based in

    Czech Republic Master Degree in Computer Science (UNIFESP) Django Developer at Kiwi.com Traveling ✈ Photography Django Girls @KatiaNakamura
  2. Tests: types • Unit Tests: ◦ Tests highly-specific parts of

    the code • Integration Tests: ◦ Tests small parts combined together @KatiaNakamura
  3. "Why should I test?" @KatiaNakamura Tests are important but… •

    "It takes time..." ⏳ • Low Priority: ⬇ ◦ there is always something more important to start/finish
  4. "Why should I test?" @KatiaNakamura • Manually test for each

    change/scenario • Integration tests: ◦ Business logic is usually more complicated Soon enough: much more time finding and fixing those nasty bugs
  5. "Why should I test?" @KatiaNakamura • Importance: ◦ guarantee that

    the last feature is working properly? ✅ ◦ start new task, stop after days/weeks and return to the previous one because of bugs you didn't catch?
  6. What you shouldn't test @KatiaNakamura • Built-in functions/libraries ◦ e.g.:

    datetime library in Python • Code built into the Framework ◦ e.g.: Model Fields in Django
  7. ◦ e.g.: custom methods for ▪ Models ▪ Views ▪

    Forms ▪ Template tags ▪ Middleware ▪ etc. @KatiaNakamura "What should I test?"
  8. @KatiaNakamura "When should I test?" • Test-After • Test-First (TDD

    - Test Driven Development) ◦ beginning: seems boring-repetitive-waste-of-time ◦ from the beginning, step-by-step with unit tests ◦ give a try
  9. Back to 2006 "As an added incentive, this is a

    feature that is present in Rails." @KatiaNakamura Testing in Django Issue #2333
  10. $ ./manage.py test @KatiaNakamura Testing in Django Testing in Django

    Highlight some points and few libraries mentioned in Ana's Talk
  11. Factory Boy @KatiaNakamura Fixtures replacement tool • it allows the

    replacement of static and hard to maintain data for randomly generated inputs. Based on factory_girl
  12. Generates fake data • better than generating random values is

    generating realist values Based on Ruby Faker - among others. @KatiaNakamura Faker
  13. @KatiaNakamura MutPy Mutation test tool • small alterations on the

    source code or bytecode • find possible weakness in the code
  14. @KatiaNakamura MutPy $ # mut.py --target node --unit-test test_node $

    mut.py --target calculator --unit-test test_calculator -m
  15. @KatiaNakamura Model Mommy Smart way to create fixtures for testing

    in Django • simple and powerful API Inspired by great OSS like Ruby ObjectDaddy and FactoryGirl
  16. High Coverage ≠ High Quality Low Coverage: we don't have

    many tests written @KatiaNakamura Test Code Coverage
  17. High Coverage ≠ High Quality Low Coverage: we don't have

    many tests written High Coverage: it doesn't mean anything... ...except that we reached a percentage of the lines of code at least once. @KatiaNakamura Test Code Coverage
  18. "When testing, more is better." but also remember to: Refactor

    Remove unnecessary tests Remove those that are not being used anymore @KatiaNakamura Test Code Coverage
  19. Refactor Remove unnecessary tests Remove those that are not being

    used anymore It's not only about writing more tests, but keeping the consistency of how everything should work. ✓ @KatiaNakamura Test Code Coverage
  20. @KatiaNakamura Top 3 tips for speeding up your tests Be

    vigilant of what gets created in setUp() • Remove and/or reduce unnecessary code Don't save model objects if not necessary • in-memory model - Employee() instead of Employee.objects.create() Isolate unit tests • more simple unit test or pytest
  21. @KatiaNakamura • Recognise the value of having tests • Start

    from the beginning with simple unit tests • Testing will become part of your day