相关疑难解决方法(0)

如何在Python中的单元测试场景中模拟HTTP请求

我想为我所有与HTTP相关的测试都包含一个Web服务器.它不需要非常复杂.我宁愿不依赖在线.所以我可以测试一下我的程序的一些选项.

  1. 启动服务器
  2. 使用适当的mime类型,响应代码等创建一些资源(URI).
  3. 运行测试(最好不要为每个测试启动服务器)
  4. 关闭服务器.

有关此代码的任何提示都会有所帮助.我用BaseHTTPServer尝试了一些但尚未成功的东西.nosetests命令似乎无限期地等待.

import unittest
from foo import core

class HttpRequests(unittest.TestCase):
    """Tests for HTTP"""

    def setUp(self):
        "Starting a Web server"
        self.port = 8080
        # Here we need to start the server
        #
        # Then define a couple of URIs and their HTTP headers
        # so we can test the code.
        pass

    def testRequestStyle(self):
        "Check if we receive a text/css content-type"
        myreq = core.httpCheck()
        myuri = 'http://127.0.0.1/style/foo'
        myua = "Foobar/1.1"
        self.asserEqual(myreq.mimetype(myuri, myua), "text/css")

    def testRequestLocation(self):
        "another test" 
        pass …
Run Code Online (Sandbox Code Playgroud)

python unit-testing monkeypatching http mocking

33
推荐指数
1
解决办法
3万
查看次数

标签 统计

http ×1

mocking ×1

monkeypatching ×1

python ×1

unit-testing ×1