Functional Testing with Python WebTest

I'm completely new to functional testing using Python WebTest, please carry me

I looked at https://webtest.readthedocs.org/en/latest/webtest.html , so I tried the code as suggested by the request:

app.get('/path', [params], [headers], [extra_environ], ...) 

Well, it looks simple enough for me. I create a test_demo.py file in the myapp folder:

  from webtest import TestApp class MyTests(): def test_admin_login(self): resp = self.TestApp.get('/admin') print (resp.request) 

Now this is where I am stuck, how do I run this test_demo.py? I tried typing in bash

  $ bin/python MyCart/mycart/test_demo.py test_admin_login 

But he does not show any result.

I am sure that something is wrong with me, but the documents do not help, or I'm just slow.

+4
source share
1 answer

You are missing a few steps.

Your program does nothing, because you did not tell it to do anything, you just defined a class. So let him tell you something to do. We will use the unittest package to make things a little more automatic.

 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 this 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 a testing app. Where to get it? Usually you need the WSGI application to be created in main via config.make_wsgi_app() . The easiest way is to download it, just like pserve development.ini , when you launch the application. We can do this through 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 you need is an INI file, similar to your development.ini , but for testing purposes. You can simply copy development.ini until you need to set any settings just for testing.

Hope this gives you a starting point to learn more about unittest .

+5
source

All Articles