listtests.py :  » Test » PyUnit » pyunit-1.4.1 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Test » PyUnit 
PyUnit » pyunit 1.4.1 » examples » listtests.py
#!/usr/bin/env python
#
# Unit tests for lists. This example shows how subclassing can be used in
# order to re-use test code wth different test objects. Comments in this
# module explain some points about typical usage. See the documentation for
# more information, including the documentation strings in the unittest module.
# 
# $Id: listtests.py,v 1.3 2001/03/12 11:52:56 purcell Exp $

import unittest
from UserList import UserList

class ListTestCase(unittest.TestCase):
    """A simple and incomplete test case for python's built-in lists"""
    def setUp(self):
        self.list = [] # All list test cases will start with an empty list

    def _appendItemsToList(self,items):
        """Do some further set-up. Used by some of the test functions."""
        for item in items: self.list.append(item)

    def testAppend(self):
        # See note in documentation concerning use of 'assert' vs. 'assert_'
        self.assert_(len(self.list) == 0)
        self.list.append('anItem')
        self.assertEquals(len(self.list), 1)
        self.assertEquals(self.list[0], 'anItem')

    def testCount(self):
  "Check count() within heterogeneous list"
        self._appendItemsToList(('a','b',1,2,'a','2'))
        self.assert_(self.list.count('a') == 2)
        # failUnless is synonymous with assert_
        self.failUnless(self.list.count('c') == 0)
        self.failUnless(self.list.count(2) == 1)
        self.failUnless(self.list.count(None) == 0)
        
    def testIndexing(self):
        # Normally when an exception is expected we would use
        # 'self.assertRaises', but this is not possible when testing behaviour
        # of operators such as []
        try:
            self.list[0]
        except IndexError: pass
        else: self.fail('expected IndexError when list empty')
        self.list.append('first')
        self.failUnless(self.list[0] == 'first')
        self.failUnless(self.list[-1] == 'first')

    def testSlicing(self):
        self.failUnless(len(self.list[:0]) == 0)
        self.failUnless(len(self.list[:1]) == 0)
        self.failUnless(len(self.list[1:]) == 0) # no IndexErrors expected
        self.failUnless(len(self.list[1:1]) == 0)
        self._appendItemsToList(('first','second','third'))
        self.failUnless(len(self.list[:1]) == 1)
        self.failUnless(type(self.list[:1]) == type(self.list))

class UserListTestCase(ListTestCase):
    """A ListTestCase subclass that tests UserLists"""
    def setUp(self):
        self.list = UserList()

def suite():
    """Returns a suite containing all the test cases in this module.
       It can be a good idea to put an identically named factory function
       like this in every test module. Such a naming convention allows
       automation of test discovery.
    """
    # Build a TestSuite containing all the possible test case instances
    # that can be made from the ListTestCase class using its 'test*'
    # functions.
    suite1 = unittest.makeSuite(ListTestCase)
    # Same with UserListTestCase, which subclasses ListTestCase; the 'test*'
    # methods in the base class will be found
    suite2 = unittest.makeSuite(UserListTestCase)
    # Make a composite test suite containing the two other suites
    return unittest.TestSuite((suite1, suite2))


if __name__ == '__main__':
    # When this module is executed from the command-line, run all its tests
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.