diff --git a/README.md b/README.md index da66993c..f439c50c 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,217 @@ Please follow the instructions in [python_testing_exercise.md](https://github.co ### pytest log +``` +========================================================== test session starts =========================================================== +platform darwin -- Python 3.11.7, pytest-7.4.0, pluggy-1.0.0 +rootdir: /Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425 +plugins: anyio-4.2.0 +collected 3 items + +tests/unit/test_diffusion2d_functions.py FFF [100%] + +================================================================ FAILURES ================================================================ +_________________________________________________________ test_initialize_domain _________________________________________________________ + + def test_initialize_domain(): + """ + Check function SolveDiffusion2D.initialize_domain + """ + solver = SolveDiffusion2D() + + w = 10. + h = 5. + dx = 0.1 + dy = 0.2 + + expected_nx = 100 + expected_ny = 25 + + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + +> assert expected_nx == solver.nx +E assert 100 == 50 +E + where 50 = .nx + +tests/unit/test_diffusion2d_functions.py:25: AssertionError +__________________________________________________ test_initialize_physical_parameters ___________________________________________________ + + def test_initialize_physical_parameters(): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + solver = SolveDiffusion2D() + + d = 5. + T_cold = 500. + T_hot = 1000. + + solver.dx = 0.1 + solver.dy = 0.2 + + expected_dt = 0.0008000000000000001 + + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) +> assert expected_dt == solver.dt +E assert 0.0008000000000000001 == 0.0016000000000000003 +E + where 0.0016000000000000003 = .dt + +tests/unit/test_diffusion2d_functions.py:44: AssertionError +---------------------------------------------------------- Captured stdout call ---------------------------------------------------------- +dt = 0.0016000000000000003 +_______________________________________________________ test_set_initial_condition _______________________________________________________ + + def test_set_initial_condition(): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + solver = SolveDiffusion2D() + + solver.nx = 3 + solver.ny = 5 + solver.dx = 4. + solver.dy = 2. + solver.T_cold = 200. + solver.T_hot = 600. + + expected_u = np.array([ + [200., 200., 200., 200., 200.], + [200., 200., 600., 600., 200.], + [200., 200., 200., 200., 200.]]) + + actual_u = solver.set_initial_condition() + +> assert((expected_u == actual_u).all()) +E assert False +E + where False = () +E + where = array([[200.,... 200., 200.]]) == array([[200.,... 200., 200.]]) +E Use -v to get more diff.all + +tests/unit/test_diffusion2d_functions.py:67: AssertionError +======================================================== short test summary info ========================================================= +FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_domain - assert 100 == 50 +FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_physical_parameters - assert 0.0008000000000000001 == 0.0016000000000000003 +FAILED tests/unit/test_diffusion2d_functions.py::test_set_initial_condition - assert False +=========================================================== 3 failed in 0.33s ============================================================ +``` + ### unittest log +``` +Fdt = 0.0016000000000000003 +FF +====================================================================== +FAIL: test_initialize_domain (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_initialize_domain) +Check function SolveDiffusion2D.initialize_domain +---------------------------------------------------------------------- +Traceback (most recent call last): + File "/Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425/tests/unit/test_diffusion2d_functions.py", line 83, in test_initialize_domain + self.assertEqual(expected_nx,self.solver.nx) +AssertionError: 100 != 50 + +====================================================================== +FAIL: test_initialize_physical_parameters (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_initialize_physical_parameters) +Checks function SolveDiffusion2D.initialize_domain +---------------------------------------------------------------------- +Traceback (most recent call last): + File "/Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425/tests/unit/test_diffusion2d_functions.py", line 100, in test_initialize_physical_parameters + self.assertAlmostEqual(expected_dt,self.solver.dt) +AssertionError: 0.0008000000000000001 != 0.0016000000000000003 within 7 places (0.0008000000000000001 difference) + +====================================================================== +FAIL: test_set_initial_condition (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_set_initial_condition) +Checks function SolveDiffusion2D.get_initial_function +---------------------------------------------------------------------- +Traceback (most recent call last): + File "/Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425/tests/unit/test_diffusion2d_functions.py", line 120, in test_set_initial_condition + self.assertTrue(np.allclose(expected_u,actual_u)) +AssertionError: False is not true + +---------------------------------------------------------------------- +Ran 3 tests in 0.001s + +FAILED (failures=3) +``` + +### integration test log + +``` +====================================================== test session starts ======================================================= +platform darwin -- Python 3.11.7, pytest-7.4.0, pluggy-1.0.0 +rootdir: /Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425 +plugins: anyio-4.2.0 +collected 2 items + +tests/integration/test_diffusion2d.py FF [100%] + +============================================================ FAILURES ============================================================ +______________________________________________ test_initialize_physical_parameters _______________________________________________ + + def test_initialize_physical_parameters(): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + solver = SolveDiffusion2D() + + w = 10. + h = 5. + dx = 0.1 + dy = 0.2 + d = 5. + T_cold = 500. + T_hot = 1000. + + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + + expected_dt = 0.0008000000000000001 + +> assert expected_dt == solver.dt +E assert 0.0008000000000000001 == 0.0016000000000000003 +E + where 0.0016000000000000003 = .dt + +tests/integration/test_diffusion2d.py:28: AssertionError +------------------------------------------------------ Captured stdout call ------------------------------------------------------ +dt = 0.0016000000000000003 +___________________________________________________ test_set_initial_condition ___________________________________________________ + + def test_set_initial_condition(): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + solver = SolveDiffusion2D() + + w = 12. + h = 10. + dx = 4. + dy = 2. + d = 4. + T_cold = 200. + T_hot = 600. + + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + actual_u = solver.set_initial_condition() + + expected_u = np.array([ + [200., 200., 200., 200., 200.], + [200., 200., 600., 600., 200.], + [200., 200., 200., 200., 200.]]) + +> assert np.allclose(actual_u, expected_u) +E assert False +E + where False = (array([[200., 200., 200., 200., 200.],\n [200., 200., 200., 200., 200.],\n [200., 200., 200., 200., 200.]]), array([[200., 200., 200., 200., 200.],\n [200., 200., 600., 600., 200.],\n [200., 200., 200., 200., 200.]])) +E + where = np.allclose + +tests/integration/test_diffusion2d.py:54: AssertionError +------------------------------------------------------ Captured stdout call ------------------------------------------------------ +dt = 0.8 +==================================================== short test summary info ===================================================== +FAILED tests/integration/test_diffusion2d.py::test_initialize_physical_parameters - assert 0.0008000000000000001 == 0.0016000000000000003 +FAILED tests/integration/test_diffusion2d.py::test_set_initial_condition - assert False +======================================================= 2 failed in 0.27s ======================================================== +``` + ## Citing The code used in this exercise is based on [Chapter 7 of the book "Learning Scientific Programming with Python"](https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/). diff --git a/coverage-report.pdf b/coverage-report.pdf new file mode 100644 index 00000000..ab59c5d1 Binary files /dev/null and b/coverage-report.pdf differ diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2d..7db10be7 100644 --- a/diffusion2d.py +++ b/diffusion2d.py @@ -38,6 +38,11 @@ def __init__(self): self.dt = None def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): + assert isinstance(w, float), "w must be a float" + assert isinstance(h, float), "h must be a float" + assert isinstance(dx, float), "dx must be a float" + assert isinstance(dy, float), "dy must be a float" + self.w = w self.h = h self.dx = dx @@ -45,7 +50,11 @@ def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): self.nx = int(w / dx) self.ny = int(h / dy) - def initialize_physical_parameters(self, d=4., T_cold=300, T_hot=700): + def initialize_physical_parameters(self, d=4., T_cold=300., T_hot=700.): + assert isinstance(d, float), "d must be a float" + assert isinstance(T_cold, float), "T_cold must be a float" + assert isinstance(T_hot, float), "T_hot must be a float" + self.D = d self.T_cold = T_cold self.T_hot = T_hot diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..806f2211 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +numpy +matplotlib \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b40..f2000aac 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -2,6 +2,7 @@ Tests for functionality checks in class SolveDiffusion2D """ +import numpy as np from diffusion2d import SolveDiffusion2D @@ -10,6 +11,21 @@ def test_initialize_physical_parameters(): Checks function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + + w = 10. + h = 5. + dx = 0.1 + dy = 0.2 + d = 5. + T_cold = 500. + T_hot = 1000. + + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + + expected_dt = 0.0008000000000000001 + + assert expected_dt == solver.dt def test_set_initial_condition(): @@ -17,3 +33,22 @@ def test_set_initial_condition(): Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + + w = 12. + h = 10. + dx = 4. + dy = 2. + d = 4. + T_cold = 200. + T_hot = 600. + + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + actual_u = solver.set_initial_condition() + + expected_u = np.array([ + [200., 200., 200., 200., 200.], + [200., 200., 600., 600., 200.], + [200., 200., 200., 200., 200.]]) + + assert np.allclose(actual_u, expected_u) diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ffd..676525b6 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -2,25 +2,122 @@ Tests for functions in class SolveDiffusion2D """ +from unittest import TestCase +import numpy as np from diffusion2d import SolveDiffusion2D +# def test_initialize_domain(): +# """ +# Check function SolveDiffusion2D.initialize_domain +# """ +# solver = SolveDiffusion2D() -def test_initialize_domain(): - """ - Check function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() +# w = 10. +# h = 5. +# dx = 0.1 +# dy = 0.2 +# expected_nx = 100 +# expected_ny = 25 -def test_initialize_physical_parameters(): - """ - Checks function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() +# solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) +# assert expected_nx == solver.nx +# assert expected_ny == solver.ny -def test_set_initial_condition(): - """ - Checks function SolveDiffusion2D.get_initial_function - """ - solver = SolveDiffusion2D() +# def test_initialize_physical_parameters(): +# """ +# Checks function SolveDiffusion2D.initialize_domain +# """ +# solver = SolveDiffusion2D() + +# d = 5. +# T_cold = 500. +# T_hot = 1000. + +# solver.dx = 0.1 +# solver.dy = 0.2 + +# expected_dt = 0.0008000000000000001 + +# solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) +# assert expected_dt == solver.dt + +# def test_set_initial_condition(): +# """ +# Checks function SolveDiffusion2D.get_initial_function +# """ +# solver = SolveDiffusion2D() + +# solver.nx = 3 +# solver.ny = 5 +# solver.dx = 4. +# solver.dy = 2. +# solver.T_cold = 200. +# solver.T_hot = 600. + +# expected_u = np.array([ +# [200., 200., 200., 200., 200.], +# [200., 200., 600., 600., 200.], +# [200., 200., 200., 200., 200.]]) + +# actual_u = solver.set_initial_condition() + +# assert((expected_u == actual_u).all()) + +class TestDiffusion2D(TestCase): + def test_initialize_domain(self): + """ + Check function SolveDiffusion2D.initialize_domain + """ + w = 10. + h = 5. + dx = 0.1 + dy = 0.2 + + expected_nx = 100 + expected_ny = 25 + + self.solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + + self.assertEqual(expected_nx,self.solver.nx) + self.assertEqual(expected_ny,self.solver.ny) + + def test_initialize_physical_parameters(self): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + d = 5. + T_cold = 500. + T_hot = 1000. + + self.solver.dx = 0.1 + self.solver.dy = 0.2 + + expected_dt = 0.0008000000000000001 + + self.solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + self.assertAlmostEqual(expected_dt,self.solver.dt) + + def test_set_initial_condition(self): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + self.solver.nx = 3 + self.solver.ny = 5 + self.solver.dx = 4. + self.solver.dy = 2. + self.solver.T_cold = 200. + self.solver.T_hot = 600. + + expected_u = np.array([ + [200., 200., 200., 200., 200.], + [200., 200., 600., 600., 200.], + [200., 200., 200., 200., 200.]]) + + actual_u = self.solver.set_initial_condition() + + self.assertTrue(np.allclose(expected_u,actual_u)) + + def setUp(self): + self.solver = SolveDiffusion2D() \ No newline at end of file diff --git a/tox.toml b/tox.toml new file mode 100644 index 00000000..fa3cc518 --- /dev/null +++ b/tox.toml @@ -0,0 +1,12 @@ +requires = ["tox>=4"] +env_list = ["pytest", "unittest"] + +[env.pytest] +description = "pytest" +deps = ["pytest", "-r requirements.txt"] +commands = [["pytest"]] + +[env.unittest] +description = "unittest" +deps = ["-r requirements.txt"] +commands = [["python", "-m", "unittest"]] \ No newline at end of file