mardi 10 novembre 2020

Pass mocked data to a Flask route only when calling the route from the browser

How can I pass mocked data to a Flask route when calling the route's URL from a Selenium web driver?

Assuming I have this route:

@main_blueprint.route('/')
def main():
    res = requests.get('some-api-url')
    name = res.text
    if res.ok:
        return render_template('main.html', name=name)
    else:
        return None

Now, I want to have a test that opens a Selenium web driver, goes to the main route (http://localhost/), and passes some mocked data (instead of calling the API) to the route and viewing it in the HTML.

I have tried the following but it doesn't seem to work.

class TestMainPage(LiveServerTestCase, unittest.TestCase):

    def create_app(self):
        with patch('app.main.requests.get') as mocked_get:
            mocked_get.return_value.ok = 'True'
            mocked_get.return_value.text = 'John'

            app = create_app()
            app.config['TESTING'] = True
            app.config.update(LIVESERVER_PORT=9898)
            return app

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get(self.get_server_url())

    def test_main_page(self):
        self.assertEqual(1, 1)

    def tearDown(self):
        self.driver.close()


if __name__ == '__main__':
    unittest.main()

I would appreciate any answer.

Aucun commentaire:

Enregistrer un commentaire