Skip to content

Commit

Permalink
Ensure no unhandled warnings are emitted during pytest run (#1540)
Browse files Browse the repository at this point in the history
* Resolve warning for markers applied to fixtures

* Resolve warning for test function with return value

* Mark test as component rather than unit since it uses a solver

* Add handling for expected warnings emitted by parameter_sweep

* Remove redundant marker registration

* Do not ignore warnings

* Treat warnings as exceptions when running pytest in CI

* Use raw string literal to escape regex char

* Move warnings-as-errors config to pytest.ini and handle IDAES/idaes-pse#1549

* Use raw string literal to resolve SyntaxErrors

* Try resolving failures in notebook tests

* Try using a more specific filter

* Add filter for yet again another ResourceWarning

* Reduce test output

* Add --cov flag previously defined unconditionally in pytest.ini

* Make filterwarnings regex Windows-compatible

* Add marker for expected warning observed on macOS CI run

* Resolve Pyomo deprecation warnings spotted in docs build log
  • Loading branch information
lbianchi-lbl authored Dec 13, 2024
1 parent f3cb0bb commit 80d9ac7
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ jobs:
if: matrix.coverage
run:
|
echo PYTEST_ADDOPTS="$PYTEST_ADDOPTS --cov-report=xml" >> $GITHUB_ENV
echo PYTEST_ADDOPTS="$PYTEST_ADDOPTS --cov --cov-report=xml" >> $GITHUB_ENV
- name: Test with pytest
run: |
pytest --pyargs watertap --idaes-flowsheets --entry-points-group watertap.flowsheets
Expand Down
14 changes: 7 additions & 7 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[pytest]
addopts = --durations=100
--cov=watertap
addopts = --durations=10
--cov-config=.coveragerc
testpaths = watertap
log_file = pytest.log
log_file_date_format = %Y-%m-%dT%H:%M:%S
log_file_format = %(asctime)s %(levelname)-7s <%(filename)s:%(lineno)d> %(message)s
log_file_level = INFO
filterwarnings =
ignore::DeprecationWarning
markers =
unit: mark test as unit tests.
component: mark test as longer, bigger, more complex than unit tests.
ui: mark test as relevant to the ui
error
# see IDAES/idaes-pse#1549
ignore:unclosed file .*Visualization-data.*SA-.*\.bin:ResourceWarning
# emitted sporadically during nbmake tests
ignore:unclosed <socket\.socket .*>:ResourceWarning
ignore:unclosed event loop .*:ResourceWarning
4 changes: 2 additions & 2 deletions watertap/core/tests/test_zero_order_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def test_set_param_from_data_no_value(self, model):
with pytest.raises(
KeyError,
match="fs.unit - no value provided for recovery_vol"
" \(index: None\) in database.",
r" \(index: None\) in database.",
):
model.fs.unit.set_param_from_data(
model.fs.unit.recovery_vol, {"recovery_vol": {}}
Expand All @@ -230,7 +230,7 @@ def test_set_param_from_data_no_units(self, model):
with pytest.raises(
KeyError,
match="fs.unit - no units provided for recovery_vol"
" \(index: None\) in database.",
r" \(index: None\) in database.",
):
model.fs.unit.set_param_from_data(
model.fs.unit.recovery_vol, {"recovery_vol": {"value": 0.42}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
)


_parameter_sweep_expected_warning = pytest.warns(
UserWarning, match="No results .* to disk .*"
)


@pytest.mark.integration
def test_monte_carlo_sampling():

Expand All @@ -38,7 +43,8 @@ def test_monte_carlo_sampling():
)

# Run the parameter sweep
global_results = run_parameter_sweep(None, seed=1)
with _parameter_sweep_expected_warning:
global_results = run_parameter_sweep(None, seed=1)

# Compare individual values for specificity
for value, truth_value in zip(global_results.flatten(), truth_values.flatten()):
Expand Down Expand Up @@ -73,13 +79,14 @@ def test_monte_carlo_sampling_with_files():
print("default_config_fpath = ", default_config_fpath)

# Run the parameter sweep
global_results = run_parameter_sweep(
seed=1,
read_sweep_params_from_file=True,
sweep_params_fname=sweep_params_fpath,
read_model_defauls_from_file=True,
defaults_fname=default_config_fpath,
)
with _parameter_sweep_expected_warning:
global_results = run_parameter_sweep(
seed=1,
read_sweep_params_from_file=True,
sweep_params_fname=sweep_params_fpath,
read_model_defauls_from_file=True,
defaults_fname=default_config_fpath,
)

# Compare individual values for specificity
for value, truth_value in zip(global_results.flatten(), truth_values.flatten()):
Expand Down Expand Up @@ -166,7 +173,8 @@ def test_lhs_sampling():
)

# Run the parameter sweep
global_results = run_parameter_sweep(seed=1, use_LHS=True)
with _parameter_sweep_expected_warning:
global_results = run_parameter_sweep(seed=1, use_LHS=True)

# Compare individual values for specificity
for value, truth_value in zip(global_results.flatten(), truth_values.flatten()):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
from idaes.core.util import DiagnosticsToolbox


# lbianchi-lbl 2024-12-12: adding this as part of watertap-org/watertap#1540
# this was only observed for macOS (EXPERIMENTAL) CI env which is slated for removal soon
@pytest.mark.filterwarnings(
"ignore:divide by zero encountered in divide:RuntimeWarning"
)
class TestASM2DFlowsheet:
@pytest.fixture(scope="class")
def model(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,20 @@
solver = get_solver()


@pytest.mark.requires_idaes_solver
class TestADM1BioPFalse:
@pytest.mark.requires_idaes_solver
@pytest.fixture(scope="class")
def system_frame(self):
m, res = main(bio_P=False)
m.results = res
return m

@pytest.mark.requires_idaes_solver
@pytest.mark.integration
def test_structure(self, system_frame):
assert_units_consistent(system_frame)
assert degrees_of_freedom(system_frame) == 0
assert_optimal_termination(system_frame.results)

@pytest.mark.requires_idaes_solver
@pytest.mark.component
def test_solve(self, system_frame):
m = system_frame
Expand Down Expand Up @@ -113,22 +111,20 @@ def test_solve(self, system_frame):
)


@pytest.mark.requires_idaes_solver
class TestADM1BioPTrue:
@pytest.mark.requires_idaes_solver
@pytest.fixture(scope="class")
def system_frame(self):
m, res = main(bio_P=True)
m.results = res
return m

@pytest.mark.requires_idaes_solver
@pytest.mark.integration
def test_structure(self, system_frame):
assert_units_consistent(system_frame)
assert degrees_of_freedom(system_frame) == 0
assert_optimal_termination(system_frame.results)

@pytest.mark.requires_idaes_solver
@pytest.mark.component
def test_solve(self, system_frame):
m = system_frame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@
)


@pytest.mark.requires_idaes_solver
class TestElectroNPFlowsheet:
@pytest.fixture(scope="class")
@pytest.mark.requires_idaes_solver
def model(self):
m, res = m, results = main(has_electroNP=True)

m.results = res

return m

@pytest.mark.requires_idaes_solver
@pytest.mark.integration
def test_structure(self, model):
assert_units_consistent(model)
assert degrees_of_freedom(model) == 0
assert_optimal_termination(model.results)

@pytest.mark.requires_idaes_solver
@pytest.mark.integration
def test_results(self, model):
# Treated water
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,20 @@
solver = get_solver()


@pytest.mark.requires_idaes_solver
class TestFullFlowsheetBioPFalse:
@pytest.mark.requires_idaes_solver
@pytest.fixture(scope="class")
def system_frame(self):
m, res = main(bio_P=False)
m.results = res
return m

@pytest.mark.requires_idaes_solver
@pytest.mark.integration
def test_structure(self, system_frame):
assert_units_consistent(system_frame)
assert degrees_of_freedom(system_frame) == 0
assert_optimal_termination(system_frame.results)

@pytest.mark.requires_idaes_solver
@pytest.mark.component
def test_solve(self, system_frame):
m = system_frame
Expand Down Expand Up @@ -112,7 +110,6 @@ def test_solve(self, system_frame):
0.00021570, rel=1e-3
)

@pytest.mark.requires_idaes_solver
@pytest.mark.component
def test_costing(self, system_frame):
m = system_frame
Expand All @@ -127,22 +124,20 @@ def test_costing(self, system_frame):
)


@pytest.mark.requires_idaes_solver
class TestFullFlowsheetBioPTrue:
@pytest.mark.requires_idaes_solver
@pytest.fixture(scope="class")
def system_frame(self):
m, res = main(bio_P=True)
m.results = res
return m

@pytest.mark.requires_idaes_solver
@pytest.mark.integration
def test_structure(self, system_frame):
assert_units_consistent(system_frame)
assert degrees_of_freedom(system_frame) == 0
assert_optimal_termination(system_frame.results)

@pytest.mark.requires_idaes_solver
@pytest.mark.component
def test_solve(self, system_frame):
m = system_frame
Expand Down Expand Up @@ -205,7 +200,6 @@ def test_solve(self, system_frame):
0.00022424, rel=1e-3
)

@pytest.mark.requires_idaes_solver
@pytest.mark.component
def test_costing(self, system_frame):
m = system_frame
Expand Down
3 changes: 2 additions & 1 deletion watertap/flowsheets/lsrro/tests/test_lssro_multi_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def test_against_multisweep(number_of_stages, tmp_path):
csv_baseline_file_name = os.path.join(
_this_file_path, "parameter_sweep_baselines", csv_file_name
)
run_case(number_of_stages, 2, output_filename=csv_test_file_name)
with pytest.warns(UserWarning, match=r"Too few points to perform interpolation\."):
run_case(number_of_stages, 2, output_filename=csv_test_file_name)

baseline = pd.read_csv(csv_baseline_file_name).astype(float).T.to_dict()
test = pd.read_csv(csv_test_file_name).astype(float).T.to_dict()
Expand Down
2 changes: 1 addition & 1 deletion watertap/tools/oli_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import time
from pyomo.common.dependencies import attempt_import

requests, requests_available = attempt_import("requests", defer_check=False)
requests, requests_available = attempt_import("requests", defer_import=False)
from watertap.tools.oli_api.util.watertap_to_oli_helper_functions import get_oli_name


Expand Down
6 changes: 4 additions & 2 deletions watertap/tools/oli_api/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@

from pyomo.common.dependencies import attempt_import

cryptography, cryptography_available = attempt_import("cryptography", defer_check=False)
cryptography, cryptography_available = attempt_import(
"cryptography", defer_import=False
)
if cryptography_available:
from cryptography.fernet import Fernet
requests, requests_available = attempt_import("requests", defer_check=False)
requests, requests_available = attempt_import("requests", defer_import=False)

_logger = logging.getLogger(__name__)
# set to info level, so user can see what is going on
Expand Down
3 changes: 1 addition & 2 deletions watertap/unit_models/tests/test_generic_desalter.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def build():
return m


@pytest.mark.unit
@pytest.mark.component
def test_solve():
m = build()
m.fs.unit.initialize()
Expand All @@ -88,4 +88,3 @@ def test_solve():
assert value(m.fs.unit.brine_solids_concentration) == pytest.approx(
180.180, rel=1e-3
)
return m
4 changes: 1 addition & 3 deletions watertap/unit_models/tests/test_generic_separator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def build():
return m


@pytest.mark.unit
@pytest.mark.component
def test_solve():
m = build()
m.fs.unit.initialize()
Expand All @@ -83,5 +83,3 @@ def test_solve():
assert value(
m.fs.unit.treated.flow_mass_phase_comp[0, "Liq", "X"]
) == pytest.approx(0.005, rel=1e-3)

return m

0 comments on commit 80d9ac7

Please sign in to comment.