Turn off sqlalchemy warnings in nosetests
Asked Answered
D

1

8

I'm trying to suppress all sqlalchemy warnings while running my test suite with nosetests. I read Turn off a warning in sqlalchemy

.............................../Users/ca/.pythonbrew/venvs/Python-2.7.3/api/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.7-x86_64.egg/sqlalchemy/engine/default.py:330: Warning: Field 'random_id' doesn't have a default value
cursor.execute(statement, parameters)

I included this in my package's __init__.py file:

def setup_package():
    """Setup the test during the whole session.

    Run by nosetests
    """
    # Suppress all SQLAlchemy warnings
    warnings.filterwarnings("ignore", category=sa_exc.SAWarning)

With the proper imports. I know it is run by nosetests because I tried some other stuff which raised error. The only thing is that it has no effect whatsoever. Warnings are still displayed.

Any idea?

Thanks!

Doze answered 20/7, 2012 at 10:36 Comment(1)
I don't believe the "default value" warning is of the SAWarning category, but rather Warning. I use the following myself: ignore:Field '\w+' doesn't have a default value:Warning and it seems to workIbbetson
M
6

It seems that nose overwrites whatever you set with:

warnings.filterwarnings("ignore")

However you can filter warnings during nose test with a command line option to nose. e.g.:

$ nosetests --logging-filter=SAWarning

I found that this still may not work under all circumstances. If this is the case you can try:

$ python -W ignore `which nosetests`
Michaella answered 22/9, 2016 at 13:25 Comment(1)
Same can be done in pytest (newer 3.1) with a pytest -W <filter> argument to pytest or as a property in pytest.ini; see docs here.Ibbetson

© 2022 - 2024 — McMap. All rights reserved.