Whoops, you're missing a few steps.
Your program doesn't do anything because you didn't tell it to do anything, you just defined a class. So let's tell it to do something. We'll use the unittest package to make things a bit more automated.
import unittest
from webtest import TestApp
class MyTests(unittest.TestCase):
def test_admin_login(self):
resp = self.TestApp.get('/admin')
print (resp.request)
if __name__ == '__main__':
unittest.main()
Run that, and we see:
E
======================================================================
ERROR: test_admin_login (__main__.MyTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_foo.py", line 6, in test_admin_login
resp = self.TestApp.get('/admin')
AttributeError: 'MyTests' object has no attribute 'TestApp'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
Ok, so we need an app to test. Where to get one? You would typically want the WSGI app that you are creating in your main
via config.make_wsgi_app()
. The easiest way is to load it up, just like pserve development.ini
does when you run your app. We can do this via pyramid.paster.get_app()
.
import unittest
from pyramid.paster import get_app
from webtest import TestApp
class MyTests(unittest.TestCase):
def test_admin_login(self):
app = get_app('testing.ini')
test_app = TestApp(app)
resp = test_app.get('/admin')
self.assertEqual(resp.status_code, 200)
if __name__ == '__main__':
unittest.main()
Now all that's needed is an INI file similar to your development.ini
, but for testing purposes. You can just copy development.ini
until you need to set any settings just for testing.
Hopefully that gives you a starting point to learn more about the unittest
package.