如何在 Python 单元测试中忽略警告(pytest / tox)
问题:
你有一个单元测试用例,如来自 UliEngineering 的这个
tests/Math/TestDecibel.py
def test_ratio_to_dB_infinite(self):
self.assertEqual(-np.inf, ratio_to_dB(0))
self.assertEqual(-np.inf, ratio_to_dB(0, factor=dBFactor.Power))
self.assertEqual(-np.inf, ratio_to_dB(-5))
self.assertEqual(-np.inf, ratio_to_dB(-5, factor=dBFactor.Power))输出警告:
output.txt
tests/Math/TestDecibel.py::TestDecibel::test_ratio_to_dB_infinite
/home/uli/UliEngineering/UliEngineering/Math/Decibel.py:29: RuntimeWarning: divide by zero encountered in log10
return np.log10(v)解决方案
你可以通过使用 pytest 注解为这个特定测试用例忽略该特定警告:
tests/Math/TestDecibel.py
import pytest
@pytest.mark.filterwarnings("ignore:divide by zero encountered in log10")
def test_ratio_to_dB_infinite(self):
self.assertEqual(-np.inf, ratio_to_dB(0))
self.assertEqual(-np.inf, ratio_to_dB(0, factor=dBFactor.Power))
self.assertEqual(-np.inf, ratio_to_dB(-5))
self.assertEqual(-np.inf, ratio_to_dB(-5, factor=dBFactor.Power))其他警告仍将被发出。
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow