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

Taming big balls of mud with functional tests

Taming big balls of mud with functional tests

Big balls of mud are software systems with poor architecture due to business pressure, developer turnover and compounding technical debt.

Using a fully fledged web application written using Django/Celery/Postgres/Redis/Cron that sends email reminders as an example, I will show the audience how they can easily write realistic test cases and do test driven development with a new testing framework, making refactoring such code easier.

Hitch is a loosely coupled, isolated open source testing framework, with a strong emphasis on functional testing. It orchestrates interacting services and enacts scenarios in isolated tests that can't easily be scripted without it (for instance, with Django's LiveServerTestCase).

These tests will involve installing a Django app in a virtualenv, interaction using a browser with selenium, mocking the passage of time, sending emails and more.

Avatar for Colm O'Connor

Colm O'Connor

June 18, 2015

Other Decks in Programming

Transcript

  1. Model View Controller Another model Hack Repeated code Repeated model

    More repeated code Yuck Bad hack Worse hack OMG
  2. Model View Controller Another model Hack Repeated code Repeated model

    More repeated code Yuck Disgusting Bad hack Worse hack OMG
  3. Model View Controller Another model Hack Repeated code Repeated model

    More repeated code Yuck Disgusting Bad hack Worse hack OMG OMFG!!
  4. Model View Controller Another model Hack Repeated code Repeated model

    More repeated code Yuck Disgusting Bad hack Worse hack OMG OMFG!! OMFGWTFBBQ!
  5. Tightly coupled code? • The degree to which individual modules

    are dependent upon one another’s implementation.
  6. Tightly coupled code? • The degree to which individual modules

    are dependent upon one another’s implementation. • Degree of ease with which you can swap one module out with another.
  7. Example code def horrible_method(like, fifty arguments): [ lots of ugly

    crap ] def nice_method(a couple or three args): [ only nice things here ] ???
  8. • The more tightly coupled your code, the more necessary

    tests become to ascertain that nothing broke when you refactor it.
  9. • The more tightly coupled your code, the more necessary

    tests become to ascertain that nothing broke when you refactor it. • ...and the more useless unit tests become at helping you do that.
  10. • The more tightly coupled your code, the more necessary

    tests become to ascertain that nothing broke when you refactor it. • ...and the more useless unit tests become at helping you do that.
  11. - engine: engine.py:DjangoReminderTestExecutionEngine name: Sign up, create reminder and wait

    for email reminder to arrive in python preconditions: python_version: 2.7 scenario: - Load website - Click: register - Fill form: id_username: django id_email: [email protected] id_password1: jazzguitar id_password2: jazzguitar - Click submit - Click: create - Fill form: id_description: Remind me about upcoming gig. id_when: 30 days - Click: create - Wait for email: Containing: Confirm E-mail Address - Confirm emails sent: 1 - Time travel: Days: 30 - Wait for email: Containing: Remind me about upcoming gig.
  12. Try it out: Prerequisites Ubuntu: $ sudo apt-get install python

    python- virtualenv python-pip postgresql redis-server git firefox node-less automake libtool Mac OS: $ brew install python redis postgresql git libtool automake npm $ npm install -g less $ pip install -U setuptools pip virtualenv
  13. Try it out $ sudo pip install hitch / pipsi

    install hitch $ git clone https://github.com/hitchtest/django-remindme.git $ cd django-remindme/ $ git clone https://github.com/hitchtest/django-remindme-tests.git $ cd django-remindme-tests/ $ hitch init $ hitch test simple_reminder.test - OR - $ hitch test simple_reminder.test --settings macos.yml
  14. Django is just the start • Hitch is an ongoing

    project. • It can test more than just Django. • It can test more than just Python. • Very simple to develop a service wrapper.