-
Notifications
You must be signed in to change notification settings - Fork 235
howto pytest warnings
Ludovico Bianchi edited this page Aug 23, 2024
·
2 revisions
- Soon ™️, the IDAES test suite will start enforcing a "no warnings" policy: unexpected warnings emitted while a test is running will cause the test to fail
- If there are warnings that are expected to be raised/emitted (either by IDAES code or third-party code) during a test, that test must be modified to properly account for these expected warnings
- Be as specific as possible
This decorates a test function, class, or module with a pytest marker that uses Python's built-in warnings filter functionality.
@pytest.mark.unit
@pytest.mark.filterwarnings("ignore")
def test_my_code():
res = np.log(-1)
Much like exceptions, warnings can have specific types. This can be specified as the third segment of the filterwarnings string format:
@pytest.mark.unit
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test_my_code():
res = np.log(-1)
The second section of the filterwarnings format can be used to specify a regular expression (regex) pattern:
@pytest.mark.unit
@pytest.mark.filterwarnings("ignore:invalid value encountered in log::RuntimeWarning")
def test_my_code():
res = np.log(-1)
Multiple variations and other more complex filters are supported with regex group constructs:
@pytest.mark.unit
@pytest.mark.filterwarnings("ignore:invalid value encountered in (log|sqrt)::RuntimeWarning")
def test_my_code():
x = np.log(-1)
y = np.sqrt(-1)
Examples in the IDAES codebase: https://github.com/search?q=repo%3AIDAES%2Fidaes-pse+pytest.mark.filterwarnings&type=code
- Set up pre-commit
- Run pytest with coverage report
- Run Pylint locally
- Update the Pyomo version
- Install Pyomo from a local Git clone
- Set up GitHub authentication with GCM
- Handle warnings in pytest