4

I am totally new to functional testing with using Python WebTest, please bear with me

I was looking at https://webtest.readthedocs.org/en/latest/webtest.html, so I tried out the code as suggested to make a request:

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

Ok, looks simple enough to me. I create a file called test_demo.py in 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 with, how should I run this test_demo.py? I've tried typing in bash

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

But it isn't showing any result.

I bet I got something all wrong, but the docs isn't helping much or I'm just plain slow.

Gino
  • 951
  • 1
  • 12
  • 24

1 Answers1

6

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.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70
  • There's a new error( ImportError: No module named pyramid.paster ) when I tried to run $ python -m unittest -v test_demo. I'm using python 3.2 – Gino Mar 07 '13 at 03:44
  • Use the python from the virtualenv in which you installed pyramid. – Michael Merickel Mar 07 '13 at 04:08
  • I'm getting an ImportError: Import by filename is not supported. I've use this command MBP:env $ bin/python -m unittest -v MyCart/mycart/test_demo – Gino Mar 07 '13 at 06:45
  • You'll have to create separate questions for this stuff since error messages are basically worthless without seeing the full traceback. Since the script is running `unittest.main()` you shouldn't need to use the `-m unittest` in your CLI. Just `env/bin/python MyCart/mycart/test_demo.py`. – Michael Merickel Mar 07 '13 at 17:00