【问题标题】:Django tests - launch multiple test servers that could communicate?Django 测试 - 启动多个可以通信的测试服务器?
【发布时间】:2016-02-11 02:12:28
【问题描述】:

我想知道是否可以使用 TestCase 或 LiveServerTestCase 运行 django 测试来模拟多个服务器的存在。

例如,我想使用 Firefox 在 localhost 端口 8081 上启动“客户端服务器”,并使用 Chrome 在端口 8082 上启动“资源服务器”。客户端服务器应该能够向资源服务器发出请求以检索 json 数据。每个服务器都应该可以使用自己的设置进行配置。简而言之,我想做类似的事情

MyTestCase(LiveServerTestCase):
    @override_settings(DATABASE={... client db config ...})
    def launch_client(self):
        self.client = webdriver.Firefox() # on port 8081

    @override_settings(DATABASE={... resource db config ...})
    def launch_resource(self):
        self.resource = webdriver.Chrome() # on port 8082   

    def test_get_json(self):
        self.client.get('http://127.0.0:8082/get/data/') # which should return data from the resource server ...

到目前为止,我已经提供了以下解决方案,但它们不起作用:

  1. 最基本的:使用 LiveServerTestCase 一次启动两个 webdrivers。即

    class MySeleniumTests(LiveServerTestCase):
    
        @classmethod
        def setUpClass(cls):
            cls.selenium_chrome = webdriver.Chrome()
            cls.selenium_firefox = webdriver.Firefox()
            super(MySeleniumTests, cls).setUpClass()
    

但这不起作用,因为两个 Web 驱动程序将在同一个端口上运行,并且不允许在每个服务器上进行不同的设置。

  1. 将 django 鼻子与多进程选项一起使用 (https://github.com/nosedjango/nosedjango#parallel-test-running-via-multiprocess)。但这只是单独运行测试。

  2. 使用此处描述的 pyvows (https://realpython.com/blog/python/asynchronous-testing-with-django-and-pyvows/)。此选项实际上在多个端口上启动多个应用程序,但会产生非常不一致的结果(线程找不到它们的服务器等)。此外,如果没有黑客攻击,从另一台服务器请求一台服务器是行不通的。

有什么想法吗?非常感谢。

【问题讨论】:

    标签: python django selenium django-testing


    【解决方案1】:

    您可以使用多个 LiveServerThread 而不是仅一个来实现自己的 LiveServerTestCase mixin。

    【讨论】:

    • 这似乎比听起来要困难得多。也许如果您启动同一服务器两次 可能 工作,但如果您想启动两个不同的服务器,您需要在启动第二个之前更改设置,并且我尝试修补 @ 987654321@ 不起作用。如果有人可以指出一些示例代码,我很乐意看到一些示例代码!
    猜你喜欢
    • 1970-01-01
    • 2010-09-07
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多