Skip to content

Instantly share code, notes, and snippets.

@jasonkeene
Last active December 15, 2015 09:28
Show Gist options
  • Save jasonkeene/5238172 to your computer and use it in GitHub Desktop.
Save jasonkeene/5238172 to your computer and use it in GitHub Desktop.
Getting started with testing in Python

Why Test

  • Tells you when your code is broken
  • Manual testing sucks
  • Prevent against regressions
    • Upgrading dependencies
    • Refactoring
  • Documentation
    • Other devs can read your tests to see how you intended your code to be used
  • Helps with design
    • Code that is hard to test is a code smell
    • Code that is easy to test knows as little as possible about the outside world

How to Write/Run Tests

  • doctest
    • fragile/ugly
    • should only be used to ensure test code in documentation is correct
  • unittest
    • xUnit style
    • lots of boilerplate/ugly self.assertEqual(x, y) ??
  • py.test/nose
    • native assertions/pretty! assert x == y
    • powerful
    • fixtures
    • useful failure messages
    • pdb integration
    • parametrize/marking tests

What kinds of Tests to Write

  • Unit tests
  • Integration tests
  • System/Functional/Acceptance tests

Testing Workflows

  • Bug fixing/regression
  • Test after
  • Test first
  • Spike

Overtime

  • mock/patching
  • coverage.py
  • demo actual test suite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment