Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
11
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

23. UNIT, INTEGRATION, AND FUNCTIONAL TESTING

usually not as easy to do with a set of integration or functional tests, but integration and functional testing provides a measure of assurance that your “units” work together, as they will be expected to when your application is run in production.

The suggested mechanism for unit and integration testing of a Pyramid application is the Python unittest module. Although this module is named unittest, it is actually capable of driving both unit and integration tests. A good unittest tutorial is available within Dive Into Python by Mark Pilgrim.

Pyramid provides a number of facilities that make unit, integration, and functional tests easier to write. The facilities become particularly useful when your code calls into Pyramid -related framework functions.

23.1 Test Set Up and Tear Down

Pyramid

uses

a “global”

(actually

thread local) data

structure to

hold

on to

two

items:

the

current request

and the

current application

registry.

These data

 

struc-

tures are available via the pyramid.threadlocal.get_current_request()

and

pyramid.threadlocal.get_current_registry() functions, respectively.

See

Thread

Locals for information about these functions and the data structures they return.

 

 

 

 

If your code uses these get_current_* functions or calls Pyramid code which uses get_current_* functions, you will need to call pyramid.testing.setUp() in your test setup and you will need to call pyramid.testing.tearDown() in your test teardown. setUp() pushes a registry onto the thread local stack, which makes the get_current_* functions work. It returns a Configurator object which can be used to perform extra configuration required by the code under test. tearDown() pops the thread local stack.

Normally when a Configurator is used directly with the main block of a Pyramid application, it defers performing any “real work” until its .commit method is called (often implicitly by the pyramid.config.Configurator.make_wsgi_app() method). The Configurator returned by setUp() is an autocommitting Configurator, however, which performs all actions implied by methods called on it immediately. This is more convenient for unit-testing purposes than needing to call pyramid.config.Configurator.commit() in each test after adding extra configuration statements.

The use of the setUp() and tearDown() functions allows you to supply each unit test method in a test case with an environment that has an isolated registry and an isolated request for the duration of a single test. Here’s an example of using this feature:

254

23.1. TEST SET UP AND TEAR DOWN

1

import unittest

2

from pyramid import testing

3

 

4

class MyTest(unittest.TestCase):

5def setUp(self):

6self.config = testing.setUp()

7

8def tearDown(self):

9testing.tearDown()

The above will make sure that get_current_registry() called within a test case method of MyTest will return the application registry associated with the config Configurator instance. Each test case method attached to MyTest will use an isolated registry.

The setUp() and tearDown() functions accepts various arguments that influence the environment of the test. See the pyramid.testing chapter for information about the extra arguments supported by these functions.

If you also want to make get_current_request() return something other than None during the course of a single test, you can pass a request object into the pyramid.testing.setUp() within the setUp method of your test:

1

2

3

4

5

6

7

8

9

10

import unittest

from pyramid import testing

class MyTest(unittest.TestCase): def setUp(self):

request = testing.DummyRequest() self.config = testing.setUp(request=request)

def tearDown(self): testing.tearDown()

If you pass

a request object into pyramid.testing.setUp() within

your test

case’s

setUp, any

test method attached to the

MyTest test

case that

directly

or

indirectly calls

get_current_request() will receive

the request

object.

Otherwise,

during

testing,

get_current_request() will return None. We use a “dummy” request implementation supplied by pyramid.testing.DummyRequest because it’s easier to construct than a “real” Pyramid request object.

255

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]