【发布时间】: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 ...
到目前为止,我已经提供了以下解决方案,但它们不起作用:
-
最基本的:使用 LiveServerTestCase 一次启动两个 webdrivers。即
class MySeleniumTests(LiveServerTestCase): @classmethod def setUpClass(cls): cls.selenium_chrome = webdriver.Chrome() cls.selenium_firefox = webdriver.Firefox() super(MySeleniumTests, cls).setUpClass()
但这不起作用,因为两个 Web 驱动程序将在同一个端口上运行,并且不允许在每个服务器上进行不同的设置。
将 django 鼻子与多进程选项一起使用 (https://github.com/nosedjango/nosedjango#parallel-test-running-via-multiprocess)。但这只是单独运行测试。
使用此处描述的 pyvows (https://realpython.com/blog/python/asynchronous-testing-with-django-and-pyvows/)。此选项实际上在多个端口上启动多个应用程序,但会产生非常不一致的结果(线程找不到它们的服务器等)。此外,如果没有黑客攻击,从另一台服务器请求一台服务器是行不通的。
有什么想法吗?非常感谢。
【问题讨论】:
标签: python django selenium django-testing