Philipp Von Weitershausen   Untested Code Is Broken Code
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Philipp Von Weitershausen Untested Code Is Broken Code

  • 5,282 views
Uploaded on

If you're one of those programmers who think that subtle bugs only happen to other people, or that only bad programmers need extensive testing, this talk is for you. ...

If you're one of those programmers who think that subtle bugs only happen to other people, or that only bad programmers need extensive testing, this talk is for you.

Any non-trivial piece of software should be accompanied by an appropriate suite of automated tests. Your Plone products are no exception. By understanding approaches to automated testing and following good working practices, you can improve the quality of your code, as well as your confidence in your own work.

In this talk, Philipp von Weitershausen and Martin Aspeli will take you through the theory and practice of unit testing, integration testing and functional testing. Through real-world examples, you will learn that testing is not only necessary: it can be fun too!

More in: Business , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,282
On Slideshare
5,277
From Embeds
5
Number of Embeds
3

Actions

Shares
Downloads
50
Comments
0
Likes
2

Embeds 5

http://www.slideshare.net 3
http://www.slideee.com 1
https://www.docsnode.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Untested code is broken code Martin Aspeli Philipp von Weitershausen
  • 2. why tests?
  • 3. I know I should write tests, but... • they take time to write • I’m a good developer • my customer / the community does the testing
  • 4. find the bug... class Employee(object): def __init__(self, name, position, employee_no=None): self.name = name self.position = position self.employee_no = employee_no salaries = {0: 12000, 1: 4000, 2: 8000, 3: 4000} def print_salary(employee): if employee.employee_no: salary = salaries.get(employee.employee_no, 0) print quot;You make EUR %s.quot; % salary else: print quot;You're not an employee currently.quot;
  • 5. find the bug... class Employee(object): def __init__(self, name, position, employee_no=None): self.name = name self.position = position self.employee_no = employee_no salaries = {0: 12000, 1: 4000, 2: 8000, 3: 4000} def print_salary(employee): if employee.employee_no: salary = salaries.get(employee.employee_no, 0) print quot;You make EUR %s.quot; % salary else: print quot;You're not an employee currently.quot;
  • 6. still asking why tests? • you rarely catch problems like these with manual testing • put the time you waste catching silly bugs, typos into writing tests • you end up saving lots of time when you refactor
  • 7. tests for print_salary Employee w/o an employee number is ignored: >>> print_salary(Employee('Adam', 'Developer')) You're not an employee currently Employee w/o a known employee number earns nothing: >>> print_salary(Employee('Berta', 'Designer', 100)) You make EUR 0. Employee w/ a valid employee number is found properly: >>> print_salary(Employee('Chris', 'CTO', 2)) You make EUR 8000.
  • 8. tests for bugs • when people report bugs, you want to make sure you can reproduce them • when you fix bugs, you want to make sure they stay fixed
  • 9. test that exercises the bug (it fails) Employee w/o an employee number is ignored: >>> print_salary(Employee('Adam', 'Developer')) You're not an employee currently Employee w/o a known employee number earns nothing: >>> print_salary(Employee('Berta', 'Designer', 100)) You make EUR 0. Employee w/ a valid employee number is found properly: >>> print_salary(Employee('Chris', 'CTO', 2)) You make EUR 8000. Zero is a valid employee number: >>> print_salary(Employee('Devon', 'CEO', 0)) You make EUR 12000
  • 10. making the test pass class Employee(object): def __init__(self, name, position, employee_no=None): self.name = name self.position = position self.employee_no = employee_no salaries = {0: 12000, 1: 4000, 2: 8000, 3: 4000} def print_salary(employee): if employee.employee_no is not None: salary = salaries.get(employee.employee_no, 0) print quot;You make EUR %s.quot; % salary else: print quot;You're not an employee currently.quot;
  • 11. test-driven development (TDD)
  • 12. write tests first • say what? • I’m serious • you don’t “forget” to write tests • you can catch design mistakes early on
  • 13. executable documentation
  • 14. tests and docs • tests should exercise APIs, demonstrate how to use them • developers may find documentation in tests • why not turn them into proper documentation?
  • 15. doctests • look like interpreter session • with text paragraphs in between • reStructuredText • can be rendered to HTML, PDF, etc.
  • 16. Interfaces are defined using Python class statements:: >>> import zope.interface >>> class IFoo(zope.interface.Interface): ... quot;quot;quot;Foo blah blahquot;quot;quot; ... ... x = zope.interface.Attribute(quot;quot;quot;X blah blahquot;quot;quot;) ... ... def bar(q, r=None): ... quot;quot;quot;bar blah blahquot;quot;quot; In the example above, we've created an interface:: >>> type(IFoo) <class 'zope.interface.interface.InterfaceClass'> We can ask for the interface's documentation:: >>> IFoo.__doc__ 'Foo blah blah'
  • 17. documentation-driven development • write doctests first • “science-fiction” • tell a story to an imaginary user • use “we” and “you” • put the story on the product homepage (e.g. plone.org, PyPI)
  • 18. Martin will now show some examples for Plone