Writing tests for the Python bug tracker

Discussion in 'Python' started by Steven D'Aprano, Mar 20, 2010.

  1. I have two reported bugs in the bug tracker waiting on tests:

    http://bugs.python.org/issue8128
    http://bugs.python.org/issue4037

    Are there any guidelines for writing tests for the standard library and
    language? I've googled, but found nothing useful: lots of guidelines for
    writing tests, and of course I've read PEP 8, but I'm not sure if there
    are conventions for tests I'm missing.



    --
    Steven
    Steven D'Aprano, Mar 20, 2010
    #1
    1. Advertising

  2. On Sat, 20 Mar 2010 06:23:14 +0000, Steven D'Aprano wrote:

    > Are there any guidelines for writing tests for the standard library and
    > language? I've googled, but found nothing useful: lots of guidelines for
    > writing tests, and of course I've read PEP 8, but I'm not sure if there
    > are conventions for tests I'm missing.


    I've found this:

    http://docs.python.org/library/test.html

    and I've written a small test:

    $ cat test_unicode_interpolation.py
    # For testing http://bugs.python.org/issue8128

    import test.test_support
    import unittest

    class K(unicode):
    def __str__(self): return "Surprise!"

    class UnicodeInterpolationTest(unittest.TestCase):
    def test_interpolation(self):
    self.assertEquals(u'%s' % K('some text'), 'Surprise!')

    def test_main():
    test.test_support.run_unittest(UnicodeInterpolationTest)

    if __name__ == "__main__":
    test_main()


    but when I try running the test, I get an error:

    $ python test_unicode_interpolation.py
    Options: {'delimiter': None}
    str of options.delimiter = None
    repr of options.delimiter = None
    len of options.delimiter
    Traceback (most recent call last):
    File "test_unicode_interpolation.py", line 3, in <module>
    import test.test_support
    File "/home/steve/python/test.py", line 8, in <module>
    print "len of options.delimiter", len(options.delimiter)
    TypeError: object of type 'NoneType' has no len()


    What am I doing wrong?



    (By the way, I'm expecting the unit test above to fail.)




    --
    Steven
    Steven D'Aprano, Mar 20, 2010
    #2
    1. Advertising

  3. Steven D'Aprano

    Guest

    On 06:52 am, wrote:
    >
    >but when I try running the test, I get an error:
    >
    >$ python test_unicode_interpolation.py
    >Options: {'delimiter': None}
    >str of options.delimiter = None
    >repr of options.delimiter = None
    >len of options.delimiter
    >Traceback (most recent call last):
    > File "test_unicode_interpolation.py", line 3, in <module>
    > import test.test_support
    > File "/home/steve/python/test.py", line 8, in <module>
    > print "len of options.delimiter", len(options.delimiter)
    >TypeError: object of type 'NoneType' has no len()
    >
    >
    >What am I doing wrong?


    Take a careful look at the stack being reported. Then, think of a
    better name than "test" for your file.

    Jean-Paul
    , Mar 20, 2010
    #3
  4. On Sat, 20 Mar 2010 07:07:58 +0000, exarkun wrote:

    >>What am I doing wrong?

    >
    > Take a careful look at the stack being reported. Then, think of a
    > better name than "test" for your file.


    Doh! *face-palm*

    I was shadowing the test package with a long forgotten test module.


    --
    Steven
    Steven D'Aprano, Mar 20, 2010
    #4
  5. On Mar 20, 6:23 am, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > I have two reported bugs in the bug tracker waiting on tests:
    >
    > http://bugs.python.org/issue8128http://bugs.python.org/issue4037
    >
    > Are there any guidelines for writing tests for the standard library and
    > language?


    Not that I can think of, beyond those you've already mentioned. I
    mostly just copy the style of existing tests (though there are
    definitely some test_*** files that aren't particularly well written).

    For quick questions, you might get good answers by asking on the
    #python-dev freenode IRC channel: a good few of the people interested
    in testing (esp. Michael Foord, Ezio Melotti) can often be found
    there.

    --
    Mark
    Mark Dickinson, Mar 20, 2010
    #5
  6. On Mar 20, 6:52 am, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > I've found this:
    >
    > http://docs.python.org/library/test.html
    >
    > and I've written a small test:
    >
    > $ cat test_unicode_interpolation.py
    > # For testinghttp://bugs.python.org/issue8128
    >
    > import test.test_support
    > import unittest
    >
    > class K(unicode):
    >     def __str__(self): return "Surprise!"
    >
    > class UnicodeInterpolationTest(unittest.TestCase):
    >     def test_interpolation(self):
    >         self.assertEquals(u'%s' % K('some text'), 'Surprise!')
    >
    > def test_main():
    >     test.test_support.run_unittest(UnicodeInterpolationTest)
    >
    > if __name__ == "__main__":
    >     test_main()


    This looks like a fine start to me. I have a feeling that the current
    fashion is for assertEqual rather than assertEquals, but I might be
    wrong. :)

    --
    Mark
    Mark Dickinson, Mar 20, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giovanni Bajo
    Replies:
    158
    Views:
    1,994
    Ben Finney
    Oct 10, 2006
  2. John Nagle

    Python bug tracker now secret?

    John Nagle, Oct 31, 2007, in forum: Python
    Replies:
    1
    Views:
    252
    Terry Reedy
    Oct 31, 2007
  3. Jean-Paul Calderone

    Re: Python bug tracker now secret?

    Jean-Paul Calderone, Oct 31, 2007, in forum: Python
    Replies:
    3
    Views:
    259
    =?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=
    Oct 31, 2007
  4. Steven D'Aprano

    Stalled ticket in Python bug tracker

    Steven D'Aprano, Jan 23, 2009, in forum: Python
    Replies:
    6
    Views:
    269
    Peter Otten
    Jan 24, 2009
  5. Brian Schröder
    Replies:
    5
    Views:
    136
    Dave Thomas
    Sep 18, 2004
Loading...

Share This Page