Search code examples
pythonpytestopenpyxluser-warning

How to ignore python UserWarning in pytest?


I am using openpyxl for parsing .xlsm files, and pytest for testing.

When I open a file, I get the:
OpenPyxl -> UserWarning: Data Validation extension is not supported and will be removed

That is not really a problem bcs. program works and I can't change the .xlsm file to fix that.

But... When I run pytest with something like:

def test_wrong_file_format():  
    assert check_excel(open_excel('file.xlsm')) == True

I will get the warning i mentioned altought check_excel(open_excel('file.xlsm')) returns True and the test should suceed...

Is there a nice way to tell the pytest that "It's not a bug it's a feature" and tests should pass even when they get this warning?

Is there other way than using something like:

with pytest.warns(UserWarning):
    warnings.warn("my warning", UserWarning)

Thank you,
Tom


Solution

  • According to the official documentation (pytest), @pytest.mark.filterwarnings is the right approach. Just choose the correct params, for example:

    @pytest.mark.filterwarnings('ignore::UserWarning')