From 78c24106f45f9f04787856ed1908710ce210356c Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:53:55 +0530 Subject: [PATCH] Removing all instances of unittest (#4472) * Removing all instances of unittest Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> * style: pre-commit fixes * Adding ruff rules Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --------- Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Co-authored-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- pyproject.toml | 2 + .../test_lithium_ion/test_compare_outputs.py | 12 +--- .../test_interface/test_butler_volmer.py | 57 +++++++------------ tests/testcase.py | 17 ------ .../test_concatenations.py | 7 +-- .../test_expression_tree/test_functions.py | 5 +- .../test_input_parameter.py | 5 +- .../test_operations/test_evaluate_python.py | 11 ---- .../test_parameter_sets/test_Ecker2015.py | 18 ++---- 9 files changed, 33 insertions(+), 101 deletions(-) delete mode 100644 tests/testcase.py diff --git a/pyproject.toml b/pyproject.toml index d2c487ede4..328388bed7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -196,6 +196,8 @@ extend-select = [ "YTT", # flake8-2020 "TID252", # relative-imports "S101", # to identify use of assert statement + "PT027", # remove unittest style assertion + "PT009", # Use pytest.raises instead of unittest-style ] ignore = [ "E741", # Ambiguous variable name diff --git a/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs.py b/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs.py index 57067d6e3b..8ae7393f83 100644 --- a/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs.py +++ b/tests/integration/test_models/test_full_battery_models/test_lithium_ion/test_compare_outputs.py @@ -4,11 +4,10 @@ import pybamm import numpy as np -import unittest from tests import StandardOutputComparison -class TestCompareOutputs(unittest.TestCase): +class TestCompareOutputs: def test_compare_outputs_surface_form(self): # load models options = [ @@ -142,12 +141,3 @@ def test_compare_narrow_size_distribution(self): # compare outputs comparison = StandardOutputComparison(solutions) comparison.test_all(skip_first_timestep=True) - - -if __name__ == "__main__": - print("Add -v for more debug output") - import sys - - if "-v" in sys.argv: - debug = True - unittest.main() diff --git a/tests/integration/test_models/test_submodels/test_interface/test_butler_volmer.py b/tests/integration/test_models/test_submodels/test_interface/test_butler_volmer.py index f1f02350cd..7b2100792e 100644 --- a/tests/integration/test_models/test_submodels/test_interface/test_butler_volmer.py +++ b/tests/integration/test_models/test_submodels/test_interface/test_butler_volmer.py @@ -2,15 +2,15 @@ # Tests for the electrode-electrolyte interface equations # +import pytest import pybamm from tests import get_discretisation_for_testing -import unittest import numpy as np -class TestButlerVolmer(unittest.TestCase): - def setUp(self): +class TestButlerVolmer: + def setup_method(self): self.delta_phi_s_n = pybamm.Variable( "surface potential difference [V]", ["negative electrode"], @@ -73,7 +73,7 @@ def setUp(self): "reaction source terms [A.m-3]": 1, } - def tearDown(self): + def teardown_method(self): del self.variables del self.c_e_n del self.c_e_p @@ -114,12 +114,12 @@ def test_creation(self): ] # negative electrode Butler-Volmer is Multiplication - self.assertIsInstance(j_n, pybamm.Multiplication) - self.assertEqual(j_n.domain, ["negative electrode"]) + assert isinstance(j_n, pybamm.Multiplication) + assert j_n.domain == ["negative electrode"] # positive electrode Butler-Volmer is Multiplication - self.assertIsInstance(j_p, pybamm.Multiplication) - self.assertEqual(j_p.domain, ["positive electrode"]) + assert isinstance(j_p, pybamm.Multiplication) + assert j_p.domain == ["positive electrode"] def test_set_parameters(self): param = pybamm.LithiumIonParameters() @@ -159,9 +159,9 @@ def test_set_parameters(self): j_p = parameter_values.process_symbol(j_p) # Test for x in j_n.pre_order(): - self.assertNotIsInstance(x, pybamm.Parameter) + assert not isinstance(x, pybamm.Parameter) for x in j_p.pre_order(): - self.assertNotIsInstance(x, pybamm.Parameter) + assert not isinstance(x, pybamm.Parameter) def test_discretisation(self): param = pybamm.LithiumIonParameters() @@ -219,17 +219,13 @@ def test_discretisation(self): [mesh["negative electrode"].nodes, mesh["positive electrode"].nodes] ) y = np.concatenate([submesh**2, submesh**3, submesh**4]) - self.assertEqual( - j_n.evaluate(None, y).shape, (mesh["negative electrode"].npts, 1) - ) - self.assertEqual( - j_p.evaluate(None, y).shape, (mesh["positive electrode"].npts, 1) - ) + assert j_n.evaluate(None, y).shape == (mesh["negative electrode"].npts, 1) + assert j_p.evaluate(None, y).shape == (mesh["positive electrode"].npts, 1) # test concatenated butler-volmer whole_cell = ["negative electrode", "separator", "positive electrode"] whole_cell_mesh = disc.mesh[whole_cell] - self.assertEqual(j.evaluate(None, y).shape, (whole_cell_mesh.npts, 1)) + assert j.evaluate(None, y).shape == (whole_cell_mesh.npts, 1) def test_diff_c_e_lead_acid(self): # With intercalation @@ -361,27 +357,12 @@ def j_p(delta_phi): j_n_FD = parameter_values.process_symbol( (j_n(delta_phi + h) - j_n(delta_phi - h)) / (2 * h) ) - self.assertAlmostEqual( - j_n_diff.evaluate(inputs={"delta_phi": 0.5}) - / j_n_FD.evaluate(inputs={"delta_phi": 0.5}), - 1, - places=5, - ) + assert j_n_diff.evaluate(inputs={"delta_phi": 0.5}) / j_n_FD.evaluate( + inputs={"delta_phi": 0.5} + ) == pytest.approx(1, abs=1e-05) j_p_FD = parameter_values.process_symbol( (j_p(delta_phi + h) - j_p(delta_phi - h)) / (2 * h) ) - self.assertAlmostEqual( - j_p_diff.evaluate(inputs={"delta_phi": 0.5}) - / j_p_FD.evaluate(inputs={"delta_phi": 0.5}), - 1, - places=5, - ) - - -if __name__ == "__main__": - print("Add -v for more debug output") - import sys - - if "-v" in sys.argv: - debug = True - unittest.main() + assert j_p_diff.evaluate(inputs={"delta_phi": 0.5}) / j_p_FD.evaluate( + inputs={"delta_phi": 0.5} + ) == pytest.approx(1, abs=1e-05) diff --git a/tests/testcase.py b/tests/testcase.py deleted file mode 100644 index 0b9b1f5dee..0000000000 --- a/tests/testcase.py +++ /dev/null @@ -1,17 +0,0 @@ -# -# Custom TestCase class for pybamm -# -import unittest - - -class TestCase(unittest.TestCase): - """ - Custom TestCase class for PyBaMM - TO BE REMOVED - """ - - def assertDomainEqual(self, a, b): - "Check that two domains are equal, ignoring empty domains" - a_dict = {k: v for k, v in a.items() if v != []} - b_dict = {k: v for k, v in b.items() if v != []} - self.assertEqual(a_dict, b_dict) diff --git a/tests/unit/test_expression_tree/test_concatenations.py b/tests/unit/test_expression_tree/test_concatenations.py index 1d7ccef610..9d653a4d51 100644 --- a/tests/unit/test_expression_tree/test_concatenations.py +++ b/tests/unit/test_expression_tree/test_concatenations.py @@ -2,7 +2,6 @@ # Tests for the Concatenation class and subclasses # import pytest -import unittest.mock as mock from tests import assert_domain_equal @@ -377,7 +376,7 @@ def test_to_equation(self): # Test concat_sym assert pybamm.Concatenation(a, b).to_equation() == func_symbol - def test_to_from_json(self): + def test_to_from_json(self, mocker): # test DomainConcatenation mesh = get_mesh_for_testing() a = pybamm.Symbol("a", domain=["negative electrode"]) @@ -386,7 +385,7 @@ def test_to_from_json(self): json_dict = { "name": "domain_concatenation", - "id": mock.ANY, + "id": mocker.ANY, "domains": { "primary": ["negative electrode", "separator", "positive electrode"], "secondary": [], @@ -429,7 +428,7 @@ def test_to_from_json(self): np_json = { "name": "numpy_concatenation", - "id": mock.ANY, + "id": mocker.ANY, "domains": { "primary": [], "secondary": [], diff --git a/tests/unit/test_expression_tree/test_functions.py b/tests/unit/test_expression_tree/test_functions.py index 5f5324c0ae..fca7d7ee67 100644 --- a/tests/unit/test_expression_tree/test_functions.py +++ b/tests/unit/test_expression_tree/test_functions.py @@ -3,7 +3,6 @@ # import pytest -import unittest.mock as mock import numpy as np from scipy import special @@ -399,7 +398,7 @@ def test_tanh(self): abs=1e-05, ) - def test_erf(self): + def test_erf(self, mocker): a = pybamm.InputParameter("a") fun = pybamm.erf(a) assert fun.evaluate(inputs={"a": 3}) == special.erf(3) @@ -416,7 +415,7 @@ def test_erf(self): # test creation from json input_json = { "name": "erf", - "id": mock.ANY, + "id": mocker.ANY, "function": "erf", "children": [a], } diff --git a/tests/unit/test_expression_tree/test_input_parameter.py b/tests/unit/test_expression_tree/test_input_parameter.py index 87cbe79a31..884341cb4f 100644 --- a/tests/unit/test_expression_tree/test_input_parameter.py +++ b/tests/unit/test_expression_tree/test_input_parameter.py @@ -4,7 +4,6 @@ import numpy as np import pybamm import pytest -import unittest.mock as mock class TestInputParameter: @@ -49,12 +48,12 @@ def test_errors(self): with pytest.raises(KeyError): a.evaluate() - def test_to_from_json(self): + def test_to_from_json(self, mocker): a = pybamm.InputParameter("a") json_dict = { "name": "a", - "id": mock.ANY, + "id": mocker.ANY, "domain": [], "expected_size": 1, } diff --git a/tests/unit/test_expression_tree/test_operations/test_evaluate_python.py b/tests/unit/test_expression_tree/test_operations/test_evaluate_python.py index e6d8a0da83..14b980b358 100644 --- a/tests/unit/test_expression_tree/test_operations/test_evaluate_python.py +++ b/tests/unit/test_expression_tree/test_operations/test_evaluate_python.py @@ -6,7 +6,6 @@ import pybamm from tests import get_discretisation_for_testing, get_1p1d_discretisation_for_testing -import unittest import numpy as np import scipy.sparse from collections import OrderedDict @@ -746,13 +745,3 @@ def test_jax_coo_matrix(self): with pytest.raises(NotImplementedError): A.multiply(v) - - -if __name__ == "__main__": - print("Add -v for more debug output") - import sys - - if "-v" in sys.argv: - debug = True - pybamm.settings.debug_mode = True - unittest.main() diff --git a/tests/unit/test_parameters/test_parameter_sets/test_Ecker2015.py b/tests/unit/test_parameters/test_parameter_sets/test_Ecker2015.py index 4be67175d7..d703b46200 100644 --- a/tests/unit/test_parameters/test_parameter_sets/test_Ecker2015.py +++ b/tests/unit/test_parameters/test_parameter_sets/test_Ecker2015.py @@ -2,11 +2,11 @@ # Tests for O'Kane (2022) parameter set # +import pytest import pybamm -import unittest -class TestEcker2015(unittest.TestCase): +class TestEcker2015: def test_functions(self): param = pybamm.ParameterValues("Ecker2015") sto = pybamm.Scalar(0.5) @@ -40,16 +40,6 @@ def test_functions(self): } for name, value in fun_test.items(): - self.assertAlmostEqual( - param.evaluate(param[name](*value[0])), value[1], places=4 + assert param.evaluate(param[name](*value[0])) == pytest.approx( + value[1], abs=0.0001 ) - - -if __name__ == "__main__": - print("Add -v for more debug output") - import sys - - if "-v" in sys.argv: - debug = True - pybamm.settings.debug_mode = True - unittest.main()