How to get the actual pyramid request in unit testing

I have a Pyramid application that I'm still trying to learn. I have to write unit tests for it, but I don’t know how to create a query.

I see that Pyramid has a test module with DummyRequest , but it is empty, and obviously, if I pass this to the views, it will not work, because it is not filled with attributes that the request will have at runtime.

So the question is, how do you pass a request that during testing looks like a request at runtime?

+4
source share
4 answers

The thing that you need to implement when you do unit testing (which distinguishes functional tests) is that you are testing a small ā€œunitā€. This device (your opinion in this case) does not require a ā€œrealā€ request and does not require a fully functional system. In this view, there are certain hopes that an object that calls itself a "request" may have, but about this, and it, of course, does not require everything available in a real request. This is where funny or dummy objects come into play. You want to test your opinion so that you can convey something in your view using the properties needed to verify that the job is complete. Let's say you have the following configuration:

 def main(): config = Configurator() config.add_route('user', '/users/{uid}') return config.make_wsgi_app() @view_config(route_name='user', renderer='user_template.mako') def user_view(request): uid = request.matchdict['uid'] user = find_user(request, uid) if user is None: raise HTTPNotFound return {'user': user} def find_user(request, uid): return request.db.query(User).filter_by(id=uid).first() 

Great, so this is a real view, and you will notice that the request requires only 2 attributes, matchdict and db . Ok, we can do this:

 class Test_user_view(unittest.TestCase): def test_it(self): req = DummyRequest() req.db = DummyDB() req.matchdict = {'uid': '3'} result = user_view(req) self.assertEqual(result['user'].id, 3) 

Now one thing that we are not considering here is the implementation of DummyDB , but the find_user find_user might be the best approach to return a dummy value. This simplifies testing and focuses on the view itself, without clogging up when talking to the database. This is a separate test.

The other answers here describe functional testing in more detail, and you should definitely take a look at using WebTest to help ensure that your entire application works as you expect, but this is not the unit test area.

+8
source

Look at the pyramid block versus integration versus functional testing and testing the pyramid recommendations if you haven’t already. IMHO, starting with functional tests, in contrast to unit tests with DummyRequest, in many cases gives better results (i.e. it’s easier to implement and maintain). I would recommend using Webtest (examples in pyramid documents) or Selenium (or a combination of both). Using webtest will allow you to test basic functions, and tests will generally run faster than Selenium. Selenium can actually run browsers and provides more granular control. Since it launches a browser, selenium tests tend to take longer. I think a good rule of thumb is that if you just need some basic testing (for example, if you are loading a specific page) then stick with Webtest. If your test requires more control over the browser (e.g. javascript debugging), try Selenium. Check out the docs above, for example, on how to test these libraries.

+2
source

I think Brian’s answer is good, but he would add that ā€œwriting as easily verifiable code as possibleā€ is a useful mantra. To the extent that you can create modular functionality and write portable libraries that can easily be tested using tools that are independent of the queries you are familiar with, you will be happy.

Functional tests are an order of magnitude more complicated and dirtier slower and less pleasant than unit tests; The web test, as Brian mentioned, is where he is behind the pyramid. Selenium is still an order of magnitude heavier and erratic and slow. Be careful; if your tests rely on actual data, they will break in time with data changes. Mocking and good fictitious information can help with this. Use more complex and more complex forms of testing, if necessary, but not only for pleasure - you can use your architecture to some extent to reduce its need.

To answer the ā€œhow are youā€ question: if you have something like Webtest, you just do some setup, and then do something like

 response = app.get('/form.html') 

then you have a convenient response object with all the information you can wish for on it, and then you write your statements. the textbook in the documents will explain better than me.

+1
source

In the setUp function, you can create such a ā€œfakeā€ request:

 request = testing.DummyRequest() request.errors = errors.Errors([]) request.validated = {} 

Then, in one of your tests, set the parameters with which you want to test. Like this:

 request.GET['app_id'] = 'xxxxxxxxx' valid_register(request) self.assertTrue('app_id' in request.validated) 

Hope this helps

+1
source

Source: https://habr.com/ru/post/1447372/


All Articles