Flask, Blueprint Collisions and Pytest












0















I am using Flask Blueprints to create an application. I am testing it with pytest. Generating test_client in different states with pytest.fixture is causing a blueprint name collision.



This is my Flask __init__ code:



from flask import Flask
from flask_dropzone import Dropzone
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_user import UserManager
from flask_googlemaps import GoogleMaps
import os
from app.settings import app, db

# get extensions
dropzone = Dropzone()
google_maps = GoogleMaps()
migrate = Migrate()

def create_app(config_class=Config):

# initialise extensions
google_maps.init_app(app)
dropzone.init_app(app)
db.init_app(app)
migrate.init_app(app, db)

# FLASK-User Stuff
from app.models import User
user_manager = UserManager(app, db, User)

from app.errors import bp as errors_bp
app.register_blueprint(errors_bp)

from app.main import bp as main_bp
app.register_blueprint(main_bp)

return app


I am then trying to use multiple pytests to test different aspects of the application. For each I am creating a pytest.fixture to generate a test_client. For example I'm creating two fixtures for testing the app with the db in different states.



For example.



@pytest.fixture(scope='module')
def client_state1():
app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' + PATH_TO_DB_STATE_1
app.config['TESTING'] = True
client = app.test_client()
ctx = app.app_context()
ctx.push()

yield client

ctx.pop()

@pytest.fixture(scope='module')
def client_state2():
app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' + PATH_TO_DB_STATE_2
app.config['TESTING'] = True
client = app.test_client()
ctx = app.app_context()
ctx.push()

yield client

ctx.pop()


def test_with_state1(client_state1):
"""Test should pass"""
assert 1 == 1

def test_with_state2(client_state2):
"""Test should pass"""
assert 1 == 1


When I run the tests I get the following error.



AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x1a16b5d4e0> and <flask.blueprints.Blueprint object at 0x1a1633f6a0>.  Both share the same name "googlemaps".  Blueprints that are created on the fly need unique names.


I have tried splitting out tests into separate files and setting the scope of the fixture to function. But to no avail. In all cases the first test passes and the second causes the collision. What am I doing wrong?










share|improve this question























  • i think, you shouldn't use the same app 'from app.settings import app', make the app unique par create it in each fixture.

    – Ali SAID OMAR
    Nov 21 '18 at 9:11











  • Thanks Ali. I made this change and I was able to resolve the problem.

    – benkarim
    Dec 23 '18 at 19:28
















0















I am using Flask Blueprints to create an application. I am testing it with pytest. Generating test_client in different states with pytest.fixture is causing a blueprint name collision.



This is my Flask __init__ code:



from flask import Flask
from flask_dropzone import Dropzone
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_user import UserManager
from flask_googlemaps import GoogleMaps
import os
from app.settings import app, db

# get extensions
dropzone = Dropzone()
google_maps = GoogleMaps()
migrate = Migrate()

def create_app(config_class=Config):

# initialise extensions
google_maps.init_app(app)
dropzone.init_app(app)
db.init_app(app)
migrate.init_app(app, db)

# FLASK-User Stuff
from app.models import User
user_manager = UserManager(app, db, User)

from app.errors import bp as errors_bp
app.register_blueprint(errors_bp)

from app.main import bp as main_bp
app.register_blueprint(main_bp)

return app


I am then trying to use multiple pytests to test different aspects of the application. For each I am creating a pytest.fixture to generate a test_client. For example I'm creating two fixtures for testing the app with the db in different states.



For example.



@pytest.fixture(scope='module')
def client_state1():
app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' + PATH_TO_DB_STATE_1
app.config['TESTING'] = True
client = app.test_client()
ctx = app.app_context()
ctx.push()

yield client

ctx.pop()

@pytest.fixture(scope='module')
def client_state2():
app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' + PATH_TO_DB_STATE_2
app.config['TESTING'] = True
client = app.test_client()
ctx = app.app_context()
ctx.push()

yield client

ctx.pop()


def test_with_state1(client_state1):
"""Test should pass"""
assert 1 == 1

def test_with_state2(client_state2):
"""Test should pass"""
assert 1 == 1


When I run the tests I get the following error.



AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x1a16b5d4e0> and <flask.blueprints.Blueprint object at 0x1a1633f6a0>.  Both share the same name "googlemaps".  Blueprints that are created on the fly need unique names.


I have tried splitting out tests into separate files and setting the scope of the fixture to function. But to no avail. In all cases the first test passes and the second causes the collision. What am I doing wrong?










share|improve this question























  • i think, you shouldn't use the same app 'from app.settings import app', make the app unique par create it in each fixture.

    – Ali SAID OMAR
    Nov 21 '18 at 9:11











  • Thanks Ali. I made this change and I was able to resolve the problem.

    – benkarim
    Dec 23 '18 at 19:28














0












0








0








I am using Flask Blueprints to create an application. I am testing it with pytest. Generating test_client in different states with pytest.fixture is causing a blueprint name collision.



This is my Flask __init__ code:



from flask import Flask
from flask_dropzone import Dropzone
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_user import UserManager
from flask_googlemaps import GoogleMaps
import os
from app.settings import app, db

# get extensions
dropzone = Dropzone()
google_maps = GoogleMaps()
migrate = Migrate()

def create_app(config_class=Config):

# initialise extensions
google_maps.init_app(app)
dropzone.init_app(app)
db.init_app(app)
migrate.init_app(app, db)

# FLASK-User Stuff
from app.models import User
user_manager = UserManager(app, db, User)

from app.errors import bp as errors_bp
app.register_blueprint(errors_bp)

from app.main import bp as main_bp
app.register_blueprint(main_bp)

return app


I am then trying to use multiple pytests to test different aspects of the application. For each I am creating a pytest.fixture to generate a test_client. For example I'm creating two fixtures for testing the app with the db in different states.



For example.



@pytest.fixture(scope='module')
def client_state1():
app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' + PATH_TO_DB_STATE_1
app.config['TESTING'] = True
client = app.test_client()
ctx = app.app_context()
ctx.push()

yield client

ctx.pop()

@pytest.fixture(scope='module')
def client_state2():
app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' + PATH_TO_DB_STATE_2
app.config['TESTING'] = True
client = app.test_client()
ctx = app.app_context()
ctx.push()

yield client

ctx.pop()


def test_with_state1(client_state1):
"""Test should pass"""
assert 1 == 1

def test_with_state2(client_state2):
"""Test should pass"""
assert 1 == 1


When I run the tests I get the following error.



AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x1a16b5d4e0> and <flask.blueprints.Blueprint object at 0x1a1633f6a0>.  Both share the same name "googlemaps".  Blueprints that are created on the fly need unique names.


I have tried splitting out tests into separate files and setting the scope of the fixture to function. But to no avail. In all cases the first test passes and the second causes the collision. What am I doing wrong?










share|improve this question














I am using Flask Blueprints to create an application. I am testing it with pytest. Generating test_client in different states with pytest.fixture is causing a blueprint name collision.



This is my Flask __init__ code:



from flask import Flask
from flask_dropzone import Dropzone
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_user import UserManager
from flask_googlemaps import GoogleMaps
import os
from app.settings import app, db

# get extensions
dropzone = Dropzone()
google_maps = GoogleMaps()
migrate = Migrate()

def create_app(config_class=Config):

# initialise extensions
google_maps.init_app(app)
dropzone.init_app(app)
db.init_app(app)
migrate.init_app(app, db)

# FLASK-User Stuff
from app.models import User
user_manager = UserManager(app, db, User)

from app.errors import bp as errors_bp
app.register_blueprint(errors_bp)

from app.main import bp as main_bp
app.register_blueprint(main_bp)

return app


I am then trying to use multiple pytests to test different aspects of the application. For each I am creating a pytest.fixture to generate a test_client. For example I'm creating two fixtures for testing the app with the db in different states.



For example.



@pytest.fixture(scope='module')
def client_state1():
app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' + PATH_TO_DB_STATE_1
app.config['TESTING'] = True
client = app.test_client()
ctx = app.app_context()
ctx.push()

yield client

ctx.pop()

@pytest.fixture(scope='module')
def client_state2():
app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' + PATH_TO_DB_STATE_2
app.config['TESTING'] = True
client = app.test_client()
ctx = app.app_context()
ctx.push()

yield client

ctx.pop()


def test_with_state1(client_state1):
"""Test should pass"""
assert 1 == 1

def test_with_state2(client_state2):
"""Test should pass"""
assert 1 == 1


When I run the tests I get the following error.



AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x1a16b5d4e0> and <flask.blueprints.Blueprint object at 0x1a1633f6a0>.  Both share the same name "googlemaps".  Blueprints that are created on the fly need unique names.


I have tried splitting out tests into separate files and setting the scope of the fixture to function. But to no avail. In all cases the first test passes and the second causes the collision. What am I doing wrong?







python flask pytest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 8:58









benkarimbenkarim

61




61













  • i think, you shouldn't use the same app 'from app.settings import app', make the app unique par create it in each fixture.

    – Ali SAID OMAR
    Nov 21 '18 at 9:11











  • Thanks Ali. I made this change and I was able to resolve the problem.

    – benkarim
    Dec 23 '18 at 19:28



















  • i think, you shouldn't use the same app 'from app.settings import app', make the app unique par create it in each fixture.

    – Ali SAID OMAR
    Nov 21 '18 at 9:11











  • Thanks Ali. I made this change and I was able to resolve the problem.

    – benkarim
    Dec 23 '18 at 19:28

















i think, you shouldn't use the same app 'from app.settings import app', make the app unique par create it in each fixture.

– Ali SAID OMAR
Nov 21 '18 at 9:11





i think, you shouldn't use the same app 'from app.settings import app', make the app unique par create it in each fixture.

– Ali SAID OMAR
Nov 21 '18 at 9:11













Thanks Ali. I made this change and I was able to resolve the problem.

– benkarim
Dec 23 '18 at 19:28





Thanks Ali. I made this change and I was able to resolve the problem.

– benkarim
Dec 23 '18 at 19:28












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408391%2fflask-blueprint-collisions-and-pytest%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408391%2fflask-blueprint-collisions-and-pytest%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory