-1

As I know performing a functional test is done with an app context. This-for I am using this modular fixture:

@pytest.fixture(scope='module')
def test_client():
    flask_app =  create_app("ENV_FILE_LOCATION")    
    # Create a test client using the Flask application configured for testing
    with flask_app.test_client() as testing_client:
        # Establish an application context
        with flask_app.app_context():
            yield testing_client  # this is where the testing happens! 

And I am after calling the test_client to do tests on each route, for example:

c = app.test_client()
response = c.get('/test/url')
# test response

When performing the tests on some routes the process works well. However, I have a one that is calling another route I get this error

requests.exceptions.ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=5500): Max retries exceeded with url: /test/nested_route_call (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f56f3c30580>: Failed to establish a new connection: [Errno 111] Connection refused'))

It is logical since the app is not running and the call is performed to the closed app. When doing the tests while the app is running, all things goes rightly. However as I must incorporate my tests in a pre-build process, it must be closed, so is there any solution to fix the issue.

Amirov
  • 66
  • 4
  • 1
    Why does your app need to make HTTP requests to its own endpoints? Just call the underlying function directly. If you REALLY want to test something like this you need to either mock out the request or run an actual dev server instead of using the test client. – ThiefMaster Aug 31 '21 at 15:36

1 Answers1

0

As said @TheifMaster we can write a separate function for the route. However there is other solution using the live server fixture from pytest-flask. Please see: https://pytest-flask.readthedocs.io/en/stable/features.html

Amirov
  • 66
  • 4