Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 1 and Version 2 of WritingTests


Ignore:
Timestamp:
06/29/07 10:58:43 (11 years ago)
Author:
Chris Arndt
Comment:

migration notice

Legend:

Unmodified
Added
Removed
Modified
  • WritingTests

    v1 v2  
    11{{{ 
    22#!rst 
    3 **Note:** This document is written against rev776 on February 14, 2006 (not that it should matter) 
    4  
    5 ============= 
    6 Writing Tests 
    7 ============= 
    8  
    9 TurboGears tries to achieve good test coverage. This helps to keep bugs from 
    10 becoming problems and lets developers know when they break something by 
    11 changing some code that would seem to be unrelated. A patch with tests is more 
    12 likely to be accepted and will generally be accepted faster than the same 
    13 patch without tests. 
    14  
    15 What to test 
    16 ------------ 
    17  
    18 Virtually every patch that somone creates comes with an implicit contract for 
    19 its use: what parameters it takes and what its output should be. A simple 
    20 example from ``turbogears/widgets/tests/test_widgets.py``: 
    21  
    22 .. code-block:: python 
    23  
    24   def test_label(): 
    25       """Tests simple labels""" 
    26       label = widgets.Label("foo") 
    27       rendered = label.render("The Foo") 
    28       print rendered 
    29       assert """<LABEL CLASS="label" FOR="foo">The Foo</LABEL>""" == rendered 
    30  
    31 This exercises the label widget's creation interface (by setting the name to 
    32 'foo'), its render method (setting the labeltext to 'The Foo') and ensures 
    33 their correctness by checking the rendered output. 
    34  
    35 Most interfaces will be more complex and there will be considerably more 
    36 assertions. 
    37  
    38 The other main source of tests is to exercise a bug and verify that it's 
    39 fixed and remains fixed. The following test method demonstrates this 
    40 (incidentally it's from the same file as the previous example): 
    41  
    42 .. code-block:: python 
    43  
    44   def test_ticket272(): 
    45       """ TextFields with a "name" attribute = "title" should be OK """ 
    46       w = widgets.TableForm(fields=[widgets.TextField(name='title')]) 
    47       output = w.render().lower() 
    48       assert 'value' not in output 
    49  
    50 How to test 
    51 ----------- 
    52  
    53 Nose is an extension of the builtin python unittest module but provides 
    54 additional features (such as fixtures and auto-discovery and starting the 
    55 tests as soon as the first is found). For full documentation, see the `nose 
    56 documentation for test writing`_. 
    57  
    58 .. _`nose documentation for test writing` : 
    59      http://somethingaboutorange.com/mrl/projects/nose/#writing-tests 
    60  
    61 Nose defines a test case as any function or method starting with 'test' or 
    62 'Test'. All testing is done using the builtin python assert statement. If you 
    63 have a ``setup_func`` or ``teardown_func`` fixture defined, it will be run 
    64 before/after every test case.· 
    65  
    66 The only tricky part of testing comes when you're trying to test for 
    67 exceptions. The following is the easiest way to do this is: 
    68  
    69 .. code-block:: python 
    70  
    71      try: 
    72          val = Ipv4.to_python('blueberry') 
    73          assert False, "should have raised an exception" 
    74      except Invalid, e: 
    75          assert e.msg == Ipv4._messages['not_a_ip'] 
     3.. note:: This page has been migrated to http://docs.turbogears.org/1.0/Testing. 
    764}}}