When we are using 3rd packages in Python, we may see some warning messages. For example, when you use the package BeautifulSoup and provide it with an url:
import bs4
def use_bs():
bs4.BeautifulSoup('https://google.com')
if __name__ == "__main__":
use_bs()
you will see the warning below:
MarkupResemblesLocatorWarning: The input looks more like a URL than markup. You may want to use an HTTP client like requests to get the document
behind the URL, and feed that document to Beautiful Soup.
bs4.BeautifulSoup('https://google.com').txt
In this post, I am not going to discuss whether you should suppress this warning. It is discussed already here and here. I only want to summarize how you can actually disable the warning if you want.
disable warning for normal running of code#
In normal python code, you can use the warnings.filterwarnings
method from the Python standard library.
import warnings
import bs4
warnings.filterwarnings(
action='ignore',
category=bs4.MarkupResemblesLocatorWarning
)
def use_bs():
bs4.BeautifulSoup('https://google.com')
if __name__ == "__main__":
use_bs()
disable warnings when running pytest#
However, even if you disable the warning in your code, when you import the module and run test with pytest, the warning will still be shown. That is because pytest has its own mechanism to deal with the warnings.
To ignore warnings, we can set up the filterwarnings
option for pytest in pyproject.toml
:
[tool.pytest.ini_options]
filterwarnings = [
"ignore::bs4.MarkupResemblesLocatorWarning",
]
The above warning filter format is from the standard warning package:
# note the module here refers to the module that is producing the warning itself,
# not calling module.
action:message:category:module:line
What is worth noting is that, when you specify the category that is python builtin, you need to specify the full path. Otherwise, pytest is trying import the warning from Python builtin warning, and will result in an error:
ERROR: while parsing the following warning configuration:
ignore::MyWarning
This error occurred:
Traceback (most recent call last):
File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1690, in parse_warning_filter
category: Type[Warning] = _resolve_warning_category(category_)
File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1729, in _resolve_warning_category
cat = getattr(m, klass)
AttributeError: module 'builtins' has no attribute 'MyWarning'. Did you mean: 'Warning'?
The same issue has also been described in this post.
References#
- detailed pytest guide on capturing warnings: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
- pytest override existing warning filter: https://stackoverflow.com/q/75345190/6064933