Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 70 lines (43 sloc) 2.73 kb
c0075cf Alison Rowland initial page creation
arowla authored
1 ---
2 layout: default
3 title: Testing with Paste and Nose
4 ---
5
6 # Testing with Paste and Nose
7
f154393 Alison Rowland First Draft
arowla authored
8 ## Problem
9
10 You want to test your web.py application.
11
12 ## Solution
13
14 from paste.fixture import TestApp
15 from nose.tools import *
16 from code import app
17
18 class TestCode():
19 def test_index(self):
20 middleware = []
21 app = TestApp(app.wsgifunc(*middleware))
22 r = app.get('/')
23 assert_equal(r.status, 200)
24 r.mustcontain('Hello, world!')
25
26 ## Background
27
28 This example makes use of the Paste and Nose libraries. [Paste](http://pythonpaste.org/) lets you throw test requests at your application, and adds some helpful [custom methods to the response objects](http://pythonpaste.org/webtest/#the-response-object), such as mustcontain(), seen above. [Nose](http://somethingaboutorange.com/mrl/projects/nose/) makes writing and running your tests dead simple. When run from the base of your tree, it automatically finds and runs anything which is named like a test, adding necessary modules to your PYTHONPATH. This gives you the flexibility to run your tests from other directories, as well. Another benefit of Nose is that you no longer need to have every test class inherit from unittest.TestCase. Many more details are outlined on the project page.
29
30 ## Explanation
31
290fab7 Alison Rowland Added directory layout
arowla authored
32 This code resides in a file called test_code.py. The directory layout of the application looks like this:
33
34 ./
35 code.py
36 ./test
37 test_code.py
38
051848c Alison Rowland Some minor edits
arowla authored
39 Most of the code example above should be fairly self-explanatory. From our main module, code, we import app, which is defined in the usual way:
40
41 app = web.application(urls, globals())
42
43 To set up the test, we pass its wsgifunc() to Paste's TestApp, as you have already seen in the example.
44
45 app = TestApp(app.wsgifunc(*middleware))
46
47 assert_equal() is one of the methods provided by nose's utils, and works just like unittest's assertEqual().
f154393 Alison Rowland First Draft
arowla authored
48
49 ## Setting Up the Test Environment
50
51 In order to avoid kicking off web.py's webserver when we run our tests, a change is required to the line which calls run(). It normally looks something like this:
52
53 if __name__ == "__main__": app.run()
54
55 We can define an environment variable, such as WEBPY_ENV=test, when we run our tests. In that case, the above line becomes the following:
56
57 import os
58
59 def is_test():
60 if 'WEBPY_ENV' in os.environ:
61 return os.environ['WEBPY_ENV'] == 'test'
62
63 if (not is_test()) and __name__ == "__main__": app.run()
64
65 Then, it's simply a matter of running nosetests like so:
66
67 WEBPY_ENV=test nosetests
68
69 The is_test() function comes in handy for other things, such as doing conditional database commits to avoid test database pollution.
Something went wrong with that request. Please try again.