Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask-Testing using selenium and mock #153

Open
nekjir90 opened this issue Mar 19, 2021 · 0 comments
Open

Flask-Testing using selenium and mock #153

nekjir90 opened this issue Mar 19, 2021 · 0 comments

Comments

@nekjir90
Copy link

Hello
I have a problem when testing using mock

I need to go to the page and do some action. But at the same time, I want to mock the foo() function.

If I send a get request through the test client -- everything is OK; but when I open the page in the browser, the code does not mocked. How can I perform some actions on the page with selenium, and at the same time, so that have mock-code?

view.py

app = Flask(__name__)


def foo():
    print('\nNo mock!\n')


@app.route('/')
def hello_world():
    foo()
    return 'Hello, World!'

tests.py

from unittest import mock
from flask_testing import LiveServerTestCase
from selenium import webdriver


def foo():
    print('\nI am MOCK!!!\n')


class BaseClient(LiveServerTestCase):

    def create_app(self):
        from view import app

        self.test_client = app.test_client()
        return app

    def setUp(self):
        self.browser = webdriver.Firefox()

    def tearDown(self):
        self.browser.quit()

    @classmethod
    def get_server_url(cls):      
        return 'http://127.0.0.1:5000'

    @mock.patch('view.foo', side_effect=foo)
    def test_bad_mock(self, mock):
        self.browser.get(self.get_server_url())  # will be printed "No mock!", but I want to mock it!

    @mock.patch('view.foo', side_effect=foo)
    def test_correct_mock(self, mock):
        self.test_client.get('/')                # will be printed "I am MOCK!!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant