哎呀,你错过了几步.
你的程序没有做任何事情,因为你没有告诉它做任何事情,你只是定义了一个类.所以让我们告诉它做点什么.我们将使用unittest软件包使事情更加自动化.
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()
运行它,我们看到:
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)
好的,我们需要一个应用程序来测试.从哪里得到一个?您通常需要通过config.make_wsgi_app()在主体中创建的WSGI应用程序.最简单的方法是加载它,就像pserve development.ini在运行你的应用程序时那样.我们可以通过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()
现在所需要的只是一个类似于你的development.ini的INI文件,但是出于测试目的.您只需复制development.ini,直到您需要设置任何仅用于测试的设置.
希望这为您提供了一个了解有关unittest包的更多信息的起点.