diff --git a/README.md b/README.md index da66993c..165f0ea2 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,319 @@ Please follow the instructions in [python_testing_exercise.md](https://github.co ## Test logs (for submission) + ### pytest log +#### test_initialize_domain +```powershell +==================================== test session starts ===================================== +platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0 +rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425 +collected 5 items + +tests\integration\test_diffusion2d.py .. [ 40%] +tests\unit\test_diffusion2d_functions.py F.. [100%] + +========================================== FAILURES ========================================== +___________________________________ test_initialize_domain ___________________________________ + + def test_initialize_domain(): + """ + Check function SolveDiffusion2D.initialize_domain + """ + # Fixture + w=60. + h=30. + dx=0.2 + dy=0.15 + + # Expected result + nx= 300 + ny= 200 + + # Actual result + solver = SolveDiffusion2D() + solver.initialize_domain(w, h, dx, dy) + actual_nx = solver.nx + actual_ny = solver.ny + + # Test +> assert actual_nx == nx +E assert 150 == 300 + +tests\unit\test_diffusion2d_functions.py:30: AssertionError +================================== short test summary info =================================== +FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_domain - assert 150 == 300 +================================ 1 failed, 4 passed in 0.54s ================================= +``` +#### test_initialize_physical_parameters +```powershell +=============================================== test session starts ================================================ +platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0 +rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425 +collected 5 items + +tests\integration\test_diffusion2d.py .. [ 40%] +tests\unit\test_diffusion2d_functions.py .F. [100%] + +===================================================== FAILURES ===================================================== +_______________________________________ test_initialize_physical_parameters ________________________________________ + + def test_initialize_physical_parameters(): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + solver = SolveDiffusion2D() + # Fixture + d=3.5 + T_cold=200. + T_hot=600. + + # Expected result + dt= 0.00113 + + + # Actual result + solver = SolveDiffusion2D() + solver.dx = 0.1 + solver.dy = 0.2 + solver.initialize_physical_parameters(d, T_cold, T_hot) + actual_dt = pytest.approx(solver.dt, abs=0.0001) + + # Test +> assert actual_dt == dt +E assert 0.004571428571428573 ± 1.0e-04 == 0.00113 +E +E comparison failed +E Obtained: 0.00113 +E Expected: 0.004571428571428573 ± 1.0e-04 + +tests\unit\test_diffusion2d_functions.py:55: AssertionError +----------------------------------------------- Captured stdout call ----------------------------------------------- +dt = 0.004571428571428573 +============================================= short test summary info ============================================== +FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_physical_parameters - assert 0.004571428571428573 ± 1.0e-04 == 0.00113 +=========================================== 1 failed, 4 passed in 0.50s ============================================ +``` +#### test_set_initial_condition +```powershell +==================================================================================================== test session starts ==================================================================================================== +platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0 +rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425 +collected 5 items + +tests\integration\test_diffusion2d.py .. [ 40%] +tests\unit\test_diffusion2d_functions.py ..F [100%] + +========================================================================================================= FAILURES ========================================================================================================== +________________________________________________________________________________________________ test_set_initial_condition _________________________________________________________________________________________________ + + def test_set_initial_condition(): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + # Fixture + + # Expected result + u= np.array([[300., 300., 300., 300.], + [300., 300., 300., 300.], + [300., 300., 300., 300.], + [300., 300., 300., 300.], + [300., 300., 300., 300.]]) + + + # Actual result + solver = SolveDiffusion2D() + solver.nx=5 + solver.ny=4 + solver.dx=0.5 + solver.dy=0.5 + solver.T_cold=300. + solver.T_hot=900. + actual_u = solver.set_initial_condition() + + + # Test + assert actual_u.shape == u.shape +> assert np.allclose(actual_u, u, atol=50) +E assert False +E + where False = (array([[900., 900., 900., 900.],\n [900., 900., 900., 900.],\n [900., 900., 900., 900.],\n [900., 900., 900., 900.],\n [900., 900., 900., 900.]]), array([[300., 300., 300., 300.],\n [300., 300., 300., 300.],\n [300., 300., 300., 300.],\n [300., 300., 300., 300.],\n [300., 300., 300., 300.]]), atol=50) +E + where = np.allclose + +tests\unit\test_diffusion2d_functions.py:87: AssertionError +================================================================================================== short test summary info ================================================================================================== +FAILED tests/unit/test_diffusion2d_functions.py::test_set_initial_condition - assert False +================================================================================================ 1 failed, 4 passed in 0.67s ================================================================================================ +``` ### unittest log +#### test_initialize_domain +```powershell +Fdt = 0.0011428571428571432 +.. +====================================================================== +FAIL: test_initialize_domain (test_diffusion2d_functions.TestDiffusion2D.test_initialize_domain) +Check function SolveDiffusion2D.initialize_domain +---------------------------------------------------------------------- +Traceback (most recent call last): + File "C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 43, in test_initialize_domain + self.assertEqual(actual_ny, ny) +AssertionError: 4 != 200 + +---------------------------------------------------------------------- +Ran 3 tests in 0.002s + +FAILED (failures=1) +``` +#### test_initialize_physical_parameters +```powershell +.dt = 0.0019047619047619052 +F. +====================================================================== +FAIL: test_initialize_physical_parameters (test_diffusion2d_functions.TestDiffusion2D.test_initialize_physical_parameters) +Checks function SolveDiffusion2D.initialize_domain +---------------------------------------------------------------------- +Traceback (most recent call last): + File "C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 67, in test_initialize_physical_parameters + self.assertAlmostEqual(actual_dt, dt , 4) +AssertionError: 0.0019047619047619052 != 0.00113 within 4 places (0.0007747619047619053 difference) + +---------------------------------------------------------------------- +Ran 3 tests in 0.002s + +FAILED (failures=1) +``` +#### test_set_initial_condition +```powershell +.dt = 0.0011428571428571432 +.F +====================================================================== +FAIL: test_set_initial_condition (test_diffusion2d_functions.TestDiffusion2D.test_set_initial_condition) +Checks function SolveDiffusion2D.get_initial_function +---------------------------------------------------------------------- +Traceback (most recent call last): + File "C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 96, in test_set_initial_condition + self.assertTrue(np.allclose(actual_u, u)) +AssertionError: False is not true + +---------------------------------------------------------------------- +Ran 3 tests in 0.002s + +FAILED (failures=1) +``` +### Integration test log +#### test_initialize_physical_parameters +```powershell +==================================================================================================== test session starts ==================================================================================================== +platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0 +rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\integration +collected 2 items + +test_diffusion2d.py F. [100%] + +========================================================================================================= FAILURES ========================================================================================================== +____________________________________________________________________________________________ test_initialize_physical_parameters ____________________________________________________________________________________________ + + def test_initialize_physical_parameters(): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + solver = SolveDiffusion2D() + # Fixture + w=30. + h=20. + dx=0.2 + dy=0.5 + + d=3.5 + T_cold=200. + T_hot=600. + + # Expected result + dt= 0.0049 + + # Actual result + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d, T_cold, T_hot) + actual_dt = pytest.approx(solver.dt, 0.01) + + # Test +> assert actual_dt == dt +E assert 0.010204081632653062 ± 1.0e-04 == 0.0049 +E +E comparison failed +E Obtained: 0.0049 +E Expected: 0.010204081632653062 ± 1.0e-04 + +test_diffusion2d.py:39: AssertionError +--------------------------------------------------------------------------------------------------- Captured stdout call ---------------------------------------------------------------------------------------------------- +dt = 0.010204081632653062 +================================================================================================== short test summary info ================================================================================================== +FAILED test_diffusion2d.py::test_initialize_physical_parameters - assert 0.010204081632653062 ± 1.0e-04 == 0.0049 +================================================================================================ 1 failed, 1 passed in 0.55s ================================================================================================ +``` +#### test_set_initial_condition +```powershell +==================================================================================================== test session starts ==================================================================================================== +platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0 +rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\integration +collected 2 items + +test_diffusion2d.py .F [100%] + +========================================================================================================= FAILURES ========================================================================================================== +________________________________________________________________________________________________ test_set_initial_condition _________________________________________________________________________________________________ + + def test_set_initial_condition(): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + solver = SolveDiffusion2D() + # Fixture + w=15. + h=12. + dx=3. + dy=3. + + d=0.001 + T_cold=300. + T_hot=900. + + # Expected result + u= np.array([[300., 300., 300., 300.], + [300., 300., 300., 300.], + [300., 300., 900., 300.], + [300., 300., 300., 300.], + [300., 300., 300., 300.]]) + + # Actual result + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d, T_cold, T_hot) + actual_u = solver.set_initial_condition() + + # Test + assert actual_u.shape == u.shape + +> assert np.allclose(actual_u, u, 0.0001) +E assert False +E + where False = (array([[900., 300., 300., 300.],\n [900., 900., 900., 900.],\n [900., 900., 900., 900.],\n [900., 300., 300., 900.],\n [300., 300., 300., 300.]]), array([[300., 300., 300., 300.],\n [300., 300., 300., 300.],\n [300., 300., 900., 300.],\n [300., 300., 300., 300.],\n [300., 300., 300., 300.]]), 0.0001) +E + where = np.allclose + +test_diffusion2d.py:72: AssertionError +--------------------------------------------------------------------------------------------------- Captured stdout call ---------------------------------------------------------------------------------------------------- +dt = 2249.9999999999995 +================================================================================================== short test summary info ================================================================================================== +FAILED test_diffusion2d.py::test_set_initial_condition - assert False +================================================================================================ 1 failed, 1 passed in 0.50s ================================================================================================ +``` + +## tox +```cmd +python -m tox +testing: recreate env because requirements removed: pytest>=8 +testing: remove tox env folder C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\.tox\testing + testing: OK (1.25 seconds) + congratulations :) (1.36 seconds) +``` ## Citing diff --git a/coverage-report.pdf.pdf b/coverage-report.pdf.pdf new file mode 100644 index 00000000..9d9f9724 Binary files /dev/null and b/coverage-report.pdf.pdf differ diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2d..f53eefcf 100644 --- a/diffusion2d.py +++ b/diffusion2d.py @@ -38,6 +38,10 @@ def __init__(self): self.dt = None def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): + assert isinstance(w, float) + assert isinstance(h, float) + assert isinstance(dx, float) + assert isinstance(dy, float) self.w = w self.h = h self.dx = dx @@ -45,9 +49,12 @@ 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) + assert isinstance(T_cold, float) #This assertion failed (Step2) + assert isinstance(T_hot, float) #This assertion failed (Step2) self.D = d - self.T_cold = T_cold + self.T_cold = T_cold self.T_hot = T_hot # Computing a stable time step @@ -109,7 +116,8 @@ def main(): DiffusionSolver.initialize_physical_parameters() u0 = DiffusionSolver.set_initial_condition() - + + print(u0) # Number of timesteps nsteps = 101 diff --git a/requirement.txt b/requirement.txt new file mode 100644 index 00000000..ac3f2889 --- /dev/null +++ b/requirement.txt @@ -0,0 +1,2 @@ +numpy>=1.15.0 +matplotlib>=3.0.0 \ No newline at end of file diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b40..e964792a 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -1,7 +1,14 @@ """ Tests for functionality checks in class SolveDiffusion2D """ +import unittest +import numpy as np +import pytest +from unittest import TestCase +import sys +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))) from diffusion2d import SolveDiffusion2D @@ -10,6 +17,26 @@ def test_initialize_physical_parameters(): Checks function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + # Fixture + w=30. + h=20. + dx=0.2 + dy=0.5 + + d=3.5 + T_cold=200. + T_hot=600. + + # Expected result + dt= 0.0049 + + # Actual result + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d, T_cold, T_hot) + actual_dt = pytest.approx(solver.dt, 0.01) + + # Test + assert actual_dt == dt def test_set_initial_condition(): @@ -17,3 +44,29 @@ def test_set_initial_condition(): Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + # Fixture + w=15. + h=12. + dx=3. + dy=3. + + d=0.001 + T_cold=300. + T_hot=900. + + # Expected result + u= np.array([[300., 300., 300., 300.], + [300., 300., 300., 300.], + [300., 300., 900., 300.], + [300., 300., 300., 300.], + [300., 300., 300., 300.]]) + + # Actual result + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d, T_cold, T_hot) + actual_u = solver.set_initial_condition() + + # Test + assert actual_u.shape == u.shape + + assert np.allclose(actual_u, u, 0.0001) diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ffd..f73ebb78 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -1,26 +1,98 @@ +#import pytest +import numpy as np +import sys +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))) +from diffusion2d import SolveDiffusion2D +from unittest import TestCase """ Tests for functions in class SolveDiffusion2D """ -from diffusion2d import SolveDiffusion2D +class TestDiffusion2D(TestCase): + + def setUp(self): + self.solver = SolveDiffusion2D() + self.tolerance=0.0001 + return super().setUp() + + def test_initialize_domain(self): + """ + Check function SolveDiffusion2D.initialize_domain + """ + # Fixture + w=60. + h=30. + dx=0.2 + dy=0.15 + + # Expected result + nx= 300 + ny= 200 + + # Actual result + self.solver.initialize_domain(w, h, dx, dy) + actual_nx = self.solver.nx + actual_ny = self.solver.ny + + # Test + self.assertEqual(actual_nx, nx) + self.assertEqual(actual_ny, ny) + #assert actual_nx == nx + #assert actual_ny == ny + + def test_initialize_physical_parameters(self): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + # Fixture + d=3.5 + T_cold=200. + T_hot=600. + + # Expected result + dt= 0.00113 + + # Actual result + self.solver.dx = 0.1 + self.solver.dy = 0.2 + self.solver.initialize_physical_parameters(d, T_cold, T_hot) + actual_dt = self.solver.dt + #actual_dt = pytest.approx(self.solver.dt, self.tolerance) + + # Test + self.assertAlmostEqual(actual_dt, dt , 4) + #assert actual_dt == dt + + def test_set_initial_condition(self): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + # Fixture + # Expected result + u= np.array([[300., 300., 300., 300.], + [300., 300., 300., 300.], + [300., 300., 300., 300.], + [300., 300., 300., 300.], + [300., 300., 300., 300.]]) -def test_initialize_domain(): - """ - Check function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() + # Actual result + self.solver.nx=5 + self.solver.ny=4 + self.solver.dx=0.5 + self.solver.dy=0.5 + self.solver.T_cold=300. + self.solver.T_hot=900. + actual_u = self.solver.set_initial_condition() -def test_initialize_physical_parameters(): - """ - Checks function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() + + # Test + self.assertEqual(actual_u.shape, u.shape) + #assert actual_u.shape == u.shape + + self.assertTrue(np.allclose(actual_u, u)) + #assert np.allclose(actual_u, u, self.tolerance) -def test_set_initial_condition(): - """ - Checks function SolveDiffusion2D.get_initial_function - """ - solver = SolveDiffusion2D() diff --git a/tox.toml b/tox.toml new file mode 100644 index 00000000..46248145 --- /dev/null +++ b/tox.toml @@ -0,0 +1,12 @@ +requires = ["tox>=4"] +env_list = ["testing"] + +[env.pytest] +description = "Run pytest" +deps = ["pytest>=8", "-r requirements.txt"] +commands = [["python", "-m", "pytest"]] + +[env.unittest] +description = "Run unittest" +deps = ["-r requirements.txt"] +commands = ["python3", "-m", "unittest", "./tests/unit/test_diffusion2d_functions.py"] \ No newline at end of file