I am trying to get the tutorial here to work for flask-testing https://flask-testing.readthedocs.org/en/latest/ specifically under Testing with LiveServer If you want your tests done via Selenium or other headless browser like PhantomJS you can use the LiveServerTestCase:
import urllib2
from flask import Flask
from flask_testing import LiveServerTestCase
class MyTest(LiveServerTestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
# Default port is 5000
app.config['LIVESERVER_PORT'] = 8943
return app
def test_server_is_up_and_running(self):
response = urllib2.urlopen(self.get_server_url())
self.assertEqual(response.code, 200)
So I took their example and whenever I go to the page with selenium it cannot find anything at that URL. I tried printing out the URL and it is going to localhost on port 8943. I googled around and couldn't find an example of someone using these two together.
from flask import Flask
from flask.ext.testing import LiveServerTestCase
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class IntegrationTest(LiveServerTestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
app.config['LIVESERVER_PORT'] = 8943
return app
def setUp(self):
self.app = self.create_app()
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def test_get_page(self):
self.browser.get(self.get_server_url())
self.assertNotIn("The requested URL was not found on the server.", self.browser.find_element_by_tag_name("body").text)
if __name__ == '__main__':
unittest.main()