How would you mock a web application in Python (for testing a Django project)

My Django application interrupts and imports data from another HTML application. I tested every parsing function and would like to test a scanner that will go through another application. After that, I would like to do some integration tests. To make the tests as simple as possible, I want to mock the imported web application by creating a small web application that runs some hard HTML code and has all the paths I'm going to go through.

EDIT . In addition, my layout should have a little dynamic behavior - for example, to test failed and successful logins. Therefore, I can not provide only static files.

How would you create such a mock application? Subclass BaseHTTPServer ? CGI? Use some structure (like twill using Quixote)? Or would it be wise to use Django for this? This is the solution I come up with for use, but Django seems too complicated for such a problem; OTOH, another structure will be too heavy a dependency for such a small need, and BaseHTTPServer just too useful to use.

2nd EDIT : I'm not interested in mocking classes, queries, etc. This is not the approach that I want to use, and the proposal to use this approach is not an answer to me (although I am grateful to the nice people who have kindly offered me this so far). If it's hard for me to think about my question, just forget what I said about tests - how would you roughly simulate a web application using Python in general?

+4
source share
4 answers

I tried to follow @Gagandeep Singh's decision. It seemed the best and probably is a good solution in other situations, but it didn’t work for me.

The problem is that I had a Django application inside the test directory of another Django application. When I ran tests of my application using manage.py test myapp , the settings.py used was that of all projects, not the file for my mocking application. I was running Django through the management API and used multiprocessing , so I part of my problem came from such a complex interaction. Maybe I could solve it, but I decided another strategy.

I decided to override BaseHTTPServer and got some acceptable results. This is not an easy task, but I managed to start my mocking application.

0
source

I think you are mocking at the wrong level. Your unit test should not depend on an external web server at all, even if you use it specifically for the test. You must replace the urllib2.Request object (or whatever you are using, which makes the actual HTTP call) with one that returns only pre-configured data, including the corresponding responses for invalid logins.

+2
source

I would load the manual pages using wget -r (recursive loading), and then make the loaded pages available as static pages with Apache, Nginx or whatever you use as the web server.

If you do not need to view dynamic changes from your web application ...

+1
source

Seems like you need to use python mock . This allows you, for example, to patch existing command (which can call an external url) and add your own test data to it.

For tests, I feel that you should never hit the outside service. Instead, you should provide the data that you expect from this service in your own device and check if your response handler does its job.

0
source

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


All Articles