Testing technique

25 views
Skip to first unread message

Станислав Соколко

unread,
Nov 18, 2016, 12:59:40 AM11/18/16
to python-pulsar
Hello!

I would like to learn the best practise of testing pulsar applications. For example, I have a class like this:

class Web(wsgi.LazyWsgi):

    def setup(self, environ):
        return wsgi.WsgiHandler([
            wsgi.Router('/', get=self.test_page),
        ])

    async def test_page(self, request):
        # do something

And I want to test test_page method. So, I would like to run some kind of fake-http-server, make fake-http requests to it. How can it be acheived?
I've tried using test_wsgi_environ and WsgiRequest, but it doesn't always work. So, it seems to be a not-so-elegant solution.
Maybe some approach, similar to this https://github.com/quantmind/pulsar/blob/master/tests/http/base.py can be used? But it requires examples, which don't seem to be included in the installed package...
So, any your advice will be extremely helpful

lsbardel

unread,
Nov 19, 2016, 12:30:15 PM11/19/16
to python-pulsar
You have two options for testing:

client-server

Create the server in the setUpClass class method, the link you provided does give you an example (quite complex). Check https://github.com/quantmind/pulsar/blob/master/examples/calculator/tests.py for a simpler setup.
Once you have the server with a valid address you can use the HttpClient directly.

Your tearDownClass method should cleanup by removing the server. The above link show you how to do it.

dummy client-server

This is available in the master branch only. Not yet released.
In this setup, there are no sockets involved, the transport layer is replaced by dummy classes.

Create a HttpTestClient:

from pulsar.apps.test import HttpTestClient

async
def test_my_test(self):
   http
= HttpTestClient(self, wsgi_handler)
   response
= await http.get('http://fake.com/..')


where wsgi_handler is your asynchronous wsgi handler.
This option is under development, feedback welcome.

Reply all
Reply to author
Forward
0 new messages