This is how I do it. The main problem is that running docker-compose will generate a different hostname (project_container_run_x), where x is hard to understand. In the end, I just walked away from the ip address. I also guarantee that DEBUG is False, otherwise I get a bad request.
I am using StaticLiveServerTestCase as follows:
import os import socket os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = '0.0.0.0:8000' class IntegrationTests(StaticLiveServerTestCase): live_server_url = 'http://{}:8000'.format( socket.gethostbyname(socket.gethostname()) ) def setUp(self): settings.DEBUG = True self.browser = webdriver.Remote( command_executor="http://selenium:4444/wd/hub", desired_capabilities=DesiredCapabilities.CHROME ) def tearDown(self): self.browser.quit() super().tearDown() def test_home(self): self.browser.get(self.live_server_url)
My docker build file has this for selenium and extends the web container (where django works). Port 5900 is open to VNC. I like to keep it isolated in something like docker-compose.selenium.yml
version: '2' services: web: environment: SELENIUM_HOST: http://selenium:4444/wd/hub TEST_SELENIUM: 'yes' depends_on: - selenium selenium: image: selenium/standalone-chrome-debug ports: - "5900:5900"
I can run tests like
docker-compose run --rm web ./manage.py test
So, my web container is accessing selenium through the host "selenium". Selenium then accesses the web container by the ip address, which is determined on the fly.
Another problem is that the temptation is to simply use the "network" as the host name. If your docker run command launches a separate web container, this will work. However, he will not use your test database, which would make this not a great test.
Bufke
source share