简体   繁体   English

你会如何在Python中模拟一个Web应用程序(用于测试Django项目)

[英]How would you mock a web app in Python (for testing a Django project)

My app in Django scraps and imports data from another application's HTML. 我在Django中的应用程序从另一个应用程序的HTML中删除和导入数据。 I tested each parsing function and would like to test the crawler that will go through the other application, too. 我测试了每个解析函数,并希望测试将通过其他应用程序的爬虫。 After this, I'd like to make some integration tests. 在此之后,我想进行一些集成测试。 For making the tests as easy to run as possible, I want to mock the imported web application by creating a little web app that serves some hardcoded HTML and has all the paths I am going to go through. 为了使测试尽可能容易运行,我想通过创建一个小的Web应用程序来模拟导入的Web应用程序,该应用程序提供一些硬编码的HTML并具有我将要经历的所有路径。

EDIT : Also, my mock has to have some little dynamic behaviors - for example, for testing both failed and successful logins. 编辑 :此外,我的模拟必须有一些小动态行为 - 例如,测试失败和成功登录。 So I cannot provide only static files. 所以我不能只提供静态文件。

How would you create such an mock application? 你会如何创建这样的模拟应用程序? Would you subclass BaseHTTPServer ? BaseHTTPServer子类化吗? CGI? CGI? Use some framework (as does twill, using Quixote)? 使用一些框架(斜纹,使用Quixote)? Or would be reasonable to use Django for it? 或者使用Django是合理的吗? That is the solution I am cogitating to use, but Django seems to be too complex for such problem; 这是我正在考虑使用的解决方案,但Django对于这样的问题似乎太复杂了; OTOH, another framework would be a too heavy dependency for such little need, and BaseHTTPServer is just too raw to use. OTOH,另一个框架对于这样的小需求将是一个过于依赖的依赖,并且BaseHTTPServerBaseHTTPServer

2nd EDIT : I am not interested on mocking classes, requests etc. etc. That is not the approach I want to use, and a suggestion to use such approach is not an answer to me (Although I am grateful to the nice people who kindly suggested me that until now ). 第二次编辑 :我对嘲笑课程,请求等不感兴趣。这不是我想要使用的方法,并且使用这种方法的建议不是我的答案(尽管我很感激善良的人们建议我, 直到现在 )。 If it is too hard to think about my question, just forget that I talked about tests - how would you crudely simulate a web application using Python in general ? 如果是太难思考我的问题,只是忘了我谈到的测试-你会如何粗暴地模拟一般使用Python的web应用程序?

I think you're mocking at the wrong level. 我认为你在嘲笑错误的程度。 Your unit test shouldn't have to depend on an external webserver at all, even if you're running it specifically for the test. 您的单元测试不应该完全依赖于外部Web服务器,即使您专门为测试运行它也是如此。 You should be replacing the urllib2.Request object (or whatever you're using that does the actual HTTP call) with one that just returns pre-canned data, including the relevant responses for invalid logins. 您应该将urllib2.Request对象(或者您正在使用的任何实际HTTP调用)替换为仅返回预先封装数据的对象,包括无效登录的相关响应。

I would download the reference pages with wget -r (recursive download), and then made the downloaded pages available as static pages with Apache, Nginx or whatever you're using as a webserver. 我将使用wget -r (递归下载)下载参考页面,然后使用Apache,Nginx或您用作网络服务器的任何内容将下载的页面作为静态页面提供。

Unless you require to see the dynamic changes from your web application... 除非您需要查看Web应用程序的动态更改...

Sounds like you need to use python mock . 听起来你需要使用python mock This allows you to for example, patch an existing command (which may be calling an external url) and add your own test data to it. 这允许您例如patch现有命令(可能正在调用外部URL)并将自己的测试数据添加到其中。

For tests, I feel that you should never be hitting an external service. 对于测试,我觉得你永远不应该打外部服务。 You should instead provide the data you would expect from that service within a fixture of your own and test that your response handler is doing its job. 您应该在自己的夹具中提供您期望从该服务获得的数据,并测试您的响应处理程序正在执行其工作。

I tried to follow @Gagandeep Singh solution. 我试图关注@Gagandeep Singh解决方案。 This seemed to be the best one, and probably is a good solution in other situations, but it did not work for me. 这似乎是最好的,在其他情况下可能是一个很好的解决方案,但它对我不起作用。

The problem is that I had a Django app inside the test directory of another Django app. 问题是我在另一个Django应用程序的测试目录中有一个Django应用程序。 When I ran the tests of my app with manage.py test myapp , the used settings.py was the one from the whole project, not the file for my mocking app. 当我使用manage.py test myapp运行我的应用程序的manage.py test myapp ,使用的settings.py是整个项目中的一个,而不是我的模拟应用程序的文件。 I was starting Django through the management API and using multiprocessing , so I bet part of my problem came from such a complex interaction. 通过管理API启动Django并使用multiprocessing ,所以我打赌我的部分问题来自于如此复杂的交互。 Maybe I could solve it, but I just decided for another strategy. 也许我可以解决它,但我刚刚决定采用另一种策略。

I decided to override BaseHTTPServer and got some acceptable results. 我决定覆盖BaseHTTPServer并得到一些可接受的结果。 This is not an easy task but I was successful on starting my mocking app. 这不是一件容易的事,但我成功开始了我的模拟应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM