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!

Community
  • 1
  • 1
charlax
  • 25,125
  • 19
  • 60
  • 71
  • 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 work – tutuDajuju Oct 11 '17 at 12:58

1 Answers1

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`
roman
  • 453
  • 6
  • 11
  • Same can be done in pytest (newer 3.1) with a `pytest -W ` argument to pytest or as a property in pytest.ini; see [docs here](https://docs.pytest.org/en/latest/warnings.html). – tutuDajuju Oct 11 '17 at 12:59