Changeset 5276

Show
Ignore:
Timestamp:
08/27/08 22:49:10 (4 months ago)
Author:
kskuhlman
Message:

Rename testutil.TGWebTest to simply TGTest, since it's easier to remember. Also, change it so it expects either a controller set to self.root, or a WebTest? instance set to self.app.

Updated quickstart templates to create tests that use this class.

Re-closes #1762.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/identity/tests/test_identity.py

    r5274 r5276  
    156156 
    157157 
    158 class TestIdentity(testutil.TGWebTest): 
     158class TestIdentity(testutil.TGTest): 
    159159 
    160160    def setUp(self): 
     
    170170        config.configure_loggers(test_config) 
    171171        config.update(test_config['global']) 
    172         testutil.mount(IdentityRoot()) 
    173         testutil.TGWebTest.setUp(self) 
     172        self.root = IdentityRoot 
     173        testutil.TGTest.setUp(self) 
    174174        self.init_model() 
    175175 
    176176    def tearDown(self): 
    177         testutil.TGWebTest.tearDown(self) 
     177        testutil.TGTest.tearDown(self) 
    178178        config.update(self._original_config) 
    179179 
  • branches/1.1/turbogears/qstemplates/quickstart/+package+/tests/test_controllers.py_tmpl

    r4695 r5276  
    55import cherrypy 
    66 
    7 testutil.mount(root = Root()) 
     7class TestPages(testutil.TGTest): 
    88 
    9 class TestPages(unittest.TestCase): 
    10  
    11     def setUp(self): 
    12         testutil.start_server() 
    13  
    14     def tearDown(self): 
    15         """Tests for apps using identity need to stop CP/TG after each test to 
    16         stop the VisitManager thread. 
    17         See http://trac.turbogears.org/turbogears/ticket/1217 for details. 
    18         """ 
    19         testutil.stop_server() 
     9    root = Root 
    2010 
    2111    def test_method(self): 
    2212        "the index method should return a string called now" 
    2313        import types 
    24         response = testutil.go("/") 
     14        response = self.app.get("/") 
    2515        assert type(response.raw["now"]) == types.StringType 
    2616 
    2717    def test_indextitle(self): 
    2818        "The indexpage should have the right title" 
    29         response = testutil.go("/") 
     19        response = self.app.get("/") 
    3020        assert "<title>welcome to turbogears</title>" in response.body.lower() 
    3121 
     
    3323    def test_logintitle(self): 
    3424        "login page should have the right title" 
    35         response = testutil.go("/login") 
    36         assert "<title>login</title>" in response.body.lower() 
     25        response = self.app.get("/login") 
     26        assert "<title>Login</title>" in response, response 
    3727#end if 
  • branches/1.1/turbogears/qstemplates/quickstart/+package+/tests/test_model.py_tmpl

    r4245 r5276  
    1818""" 
    1919 
    20 from turbogears import testutil, database 
     20from turbogears.testutil import DBTest 
    2121 
    2222# from ${package}.model import YourModelClass, User 
    2323 
    24 # class TestUser(testutil.DBTest): 
    25 #     def get_model(self): 
    26 #         return User 
     24# class TestUser(DBTest): 
     25#     model = User 
    2726# 
    2827#     def test_creation(self): 
  • branches/1.1/turbogears/qstemplates/quickstart/setup.py_tmpl

    r4201 r5276  
    2828    install_requires=[ 
    2929        "TurboGears >= ${turbogearsversion}", 
     30        "WebTest", 
    3031#if $sqlobject == 'True' 
    3132        "$sqlobjectversion" 
  • branches/1.1/turbogears/tests/test_controllers.py

    r5241 r5276  
    243243 
    244244 
    245 class TestRoot(testutil.TGWebTest): 
     245class TestRoot(testutil.TGTest): 
    246246 
    247247    def setUp(self): 
    248248        testutil.mount(MyRoot(), '/') 
    249249        testutil.mount(SubApp(), '/subthing') 
     250        self.app = testutil.make_app() 
    250251        super(TestRoot, self).setUp() 
    251252 
     
    566567 
    567568 
    568 class TestURLs(testutil.TGWebTest): 
     569class TestURLs(testutil.TGTest): 
    569570 
    570571    def setUp(self): 
     
    572573        testutil.mount(SubApp(), '/subthing') 
    573574        testutil.mount(SubApp(), '/subthing/subsubthing') 
     575        self.app = testutil.make_app() 
    574576        super(TestURLs, self).setUp() 
    575577 
  • branches/1.1/turbogears/tests/test_paginate.py

    r5241 r5276  
    187187 
    188188 
    189 class TestSpy(testutil.TGWebTest): 
     189class TestPagination(testutil.TGTest): 
     190    """Base class for all Paginate TestCases""" 
     191 
     192    def setUp(self): 
     193        self.root = self.MyRoot 
     194        super(TestPagination, self).setUp() 
     195 
     196    def request(self, url, status=200): 
     197        response = self.app.get(url, status=status) 
     198        self.body = response.body 
     199        if "fail: " in self.body: 
     200            print self.body 
     201            assert False, "Spy alert! Check body output for details..." 
     202 
     203 
     204class TestSpy(TestPagination): 
    190205    """Never trust a spy""" 
    191206 
     
    219234            spy = Spy(foobar=10) 
    220235            return dict(data=data, spy=spy) 
    221  
    222  
    223     def setUp(self): 
    224         super(TestSpy, self).setUp(self.MyRoot) 
    225  
    226236 
    227237    def test_spy(self): 
     
    250260        Spy.assert_ok(response.body, 'var_name', 'data') 
    251261        Spy.assert_ok(response.body, 'var_name', "'data'", raw=True) 
    252  
    253  
    254 class TestPagination(testutil.TGWebTest): 
    255     """Base class for all Paginate TestCases""" 
    256  
    257     def setUp(self): 
    258         super(TestPagination, self).setUp(self.MyRoot) 
    259  
    260     def request(self, url, status=200): 
    261         response = self.app.get(url, status=status) 
    262         self.body = response.body 
    263         if "fail: " in self.body: 
    264             print self.body 
    265             assert False, "Spy alert! Check body output for details..." 
    266262 
    267263 
  • branches/1.1/turbogears/tests/test_view.py

    r4921 r5276  
    44import unittest 
    55 
    6 class TestView(testutil.TGWebTest): 
     6class TestView(unittest.TestCase): 
     7 
     8    def setUp(self): 
     9        # The server needs to be started so that the template engines get loaded. 
     10        testutil.start_server() 
    711 
    812    def test_cycle(self): 
  • branches/1.1/turbogears/testutil.py

    r5275 r5276  
    160160 
    161161 
    162 class TGWebTest(unittest.TestCase): 
     162class TGTest(unittest.TestCase): 
    163163    """A WebTest enabled unit testing class. 
    164164 
    165     This allows testers to subclass us and use self.app to make WebTest calls. 
    166  
    167     """ 
    168  
    169     def setUp(self, controller=None): 
    170         """Set up the WebTest by starting the server." 
     165    To use, subclass & set root to your controller object, or set app to a  
     166    webtest.TestApp instance.   
     167 
     168    In your tests, use self.app to make WebTest calls. 
     169    """ 
     170 
     171    root = None 
     172    app = None 
     173 
     174    def setUp(self): 
     175        """Set up the WebTest by starting the server. 
    171176 
    172177        You should override this and make sure you have properly 
     
    174179        or simply pass a root controller to super. 
    175180        Otherwise the Cherrypy filters for TurboGears will not be used. 
    176  
    177181        """ 
    178         self.app = make_app(controller) 
     182        assert self.root or self.app, "Either self.root or self.app must be set" 
     183        if not self.app:  
     184            self.app = make_app(self.root)  
    179185        start_server() 
    180186 
     
    182188        """Tear down the WebTest by stopping the server.""" 
    183189        stop_server(tg_only = True) 
    184         del self.app 
    185190 
    186191    def login_user(self, user): 
     
    285290 
    286291class DBTest(unittest.TestCase): 
    287  
     292    """A database enabled unit testing class. 
     293 
     294    Creates and destroys your database before and after each unit test.  
     295    You must set the model attribute in order for this class to  
     296    function correctly. 
     297    """ 
    288298    model = None 
    289299 
     
    298308            self.model = get_model() 
    299309            if not self.model: 
    300                 raise "Unable to run database tests without a model" 
    301  
     310                raise Exception("Unable to run database tests without a model") 
    302311        for item in self._get_soClasses(): 
    303312            if isinstance(item, types.TypeType) and issubclass(item,