diff --git a/lab11/my_sum.py b/lab11/my_sum.py new file mode 100644 index 0000000000000000000000000000000000000000..44cfd36b45ccef3992268bbe519cdccb17699388 --- /dev/null +++ b/lab11/my_sum.py @@ -0,0 +1,15 @@ +class Calculate: + + def sum(self, a, b): + return a + b + + def multiply(self, a, b): + return a * b + + def subtract(self, a, b): + return a - b + + def divide(self, a, b): + if b == 0: + raise ValueError("Division by zero is not allowed") + return a / b diff --git a/lab11/ptest/__MACOSX/._uwe_logo.png b/lab11/ptest/__MACOSX/._uwe_logo.png new file mode 100644 index 0000000000000000000000000000000000000000..0ea62ea99b4fc0b23d2f9b41a3db4fdf23cb5d24 Binary files /dev/null and b/lab11/ptest/__MACOSX/._uwe_logo.png differ diff --git a/lab11/ptest/piltest.py b/lab11/ptest/piltest.py new file mode 100644 index 0000000000000000000000000000000000000000..8ed7876874dfa312215154bf79e8b235d1eb9b8b --- /dev/null +++ b/lab11/ptest/piltest.py @@ -0,0 +1,28 @@ +# This program will test the installation of tkinter/PIL +# If you see a UWE logo and a button then it is working + +# Use this to test the setup on any machnes you will be using for ASD development + +import tkinter as tk +import tkinter.messagebox as tkm +from PIL import ImageTk, Image + +window = tk.Tk() + +def buttonPressed(): + tkm.showinfo("Alert!","Hooray!") + +label = tk.Label(text="If you see the UWE logo below PIL is working!"); +label.pack() + +button = tk.Button(master=window, text="OK",command=buttonPressed) +button.pack() + +c = tk.Canvas(master=window,bg="red",width=700, height=350) + +# Use an absolute path to the image file +im = tk.PhotoImage(file = r"d:\UWE\2nd Year\2nd semester\Advanced Software Dev\twinkter lab\lab11\ptest\uwe_logo.png") +image = c.create_image(0, 0,anchor="nw", image=im) + +c.pack() +window.mainloop() \ No newline at end of file diff --git a/lab11/ptest/uwe_logo.png b/lab11/ptest/uwe_logo.png new file mode 100644 index 0000000000000000000000000000000000000000..a22d6bd7de2df56f91992284bee6d9a7770460d3 Binary files /dev/null and b/lab11/ptest/uwe_logo.png differ diff --git a/lab11/study_unittest/basic.ipynb b/lab11/study_unittest/basic.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..aa59a16d05aa00bab6dafb526064394dc51e392e --- /dev/null +++ b/lab11/study_unittest/basic.ipynb @@ -0,0 +1,82 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Everything passed\n" + ] + } + ], + "source": [ + "# test_sum_list\n", + "\n", + "def test_sum():\n", + " assert sum([1, 2, 3]) == 6, \"Should be 6\"\n", + "\n", + "if __name__ == \"__main__\":\n", + " test_sum()\n", + " print(\"Everything passed\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "Should be 6", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[2], line 11\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m 10\u001b[0m test_sum()\n\u001b[1;32m---> 11\u001b[0m \u001b[43mtest_sum_tuple\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 12\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEverything passed\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "Cell \u001b[1;32mIn[2], line 7\u001b[0m, in \u001b[0;36mtest_sum_tuple\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtest_sum_tuple\u001b[39m():\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28msum\u001b[39m((\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m2\u001b[39m)) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m6\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mShould be 6\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", + "\u001b[1;31mAssertionError\u001b[0m: Should be 6" + ] + } + ], + "source": [ + "# test_sum_tuple\n", + "\n", + "def test_sum():\n", + " assert sum([1, 2, 3]) == 6, \"Should be 6\"\n", + "\n", + "def test_sum_tuple():\n", + " assert sum((1, 2, 2)) == 6, \"Should be 6\"\n", + "\n", + "if __name__ == \"__main__\":\n", + " test_sum()\n", + " test_sum_tuple()\n", + " print(\"Everything passed\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/lab11/study_unittest/inc_dec.py b/lab11/study_unittest/inc_dec.py new file mode 100644 index 0000000000000000000000000000000000000000..d7c396cff438afd1f3d8b0a6db5af425817af449 --- /dev/null +++ b/lab11/study_unittest/inc_dec.py @@ -0,0 +1,5 @@ +def increment(x): + return x + 1 + +def decrement(x): + return x - 1 diff --git a/lab11/study_unittest/test_unittest.py b/lab11/study_unittest/test_unittest.py new file mode 100644 index 0000000000000000000000000000000000000000..c09d62ecb621be19e0f881796e595b2b5a9b5a42 --- /dev/null +++ b/lab11/study_unittest/test_unittest.py @@ -0,0 +1,13 @@ +import inc_dec # The code to test +import unittest # The test framework + +class Test_TestIncrementDecrement(unittest.TestCase): + def test_increment(self): + self.assertEqual(inc_dec.increment(3), 4) + + # This test is designed to fail for demonstration purposes. + def test_decrement(self): + self.assertEqual(inc_dec.decrement(3), 4) + +if __name__ == '__main__': + unittest.main() diff --git a/lab11/test_my_sum.py b/lab11/test_my_sum.py new file mode 100644 index 0000000000000000000000000000000000000000..79c5092beedbd2f43c093f42387e5f655312eca5 --- /dev/null +++ b/lab11/test_my_sum.py @@ -0,0 +1,98 @@ +import unittest +from my_sum import Calculate + +class TestCalculate(unittest.TestCase): + def setUp(self): + self.calc = Calculate() # Create instance + + # 5 Correct Expected Values + def test_sum_positive(self): + """Test sum with two positive numbers""" + self.assertEqual(self.calc.sum(3, 4), 7) + + def test_sum_negative(self): + """Test sum with two negative numbers""" + self.assertEqual(self.calc.sum(-2, -3), -5) + + def test_sum_mixed(self): + """Test sum with positive and negative numbers""" + self.assertEqual(self.calc.sum(5, -2), 3) + + def test_multiply_positive(self): + """Test multiply with two positive numbers""" + self.assertEqual(self.calc.multiply(2, 3), 6) + + def test_multiply_negative(self): + """Test multiply with positive and negative numbers""" + self.assertEqual(self.calc.multiply(4, -2), -8) + + # 5 Incorrect Expected Values + def test_sum_wrong_positive(self): + """Test sum with incorrect positive result""" + self.assertEqual(self.calc.sum(3, 4), 8) # Should be 7 + + def test_sum_wrong_negative(self): + """Test sum with incorrect negative result""" + self.assertEqual(self.calc.sum(-2, -3), -4) # Should be -5 + + def test_sum_wrong_zero(self): + """Test sum with incorrect zero result""" + self.assertEqual(self.calc.sum(5, -5), 1) # Should be 0 + + def test_multiply_wrong_positive(self): + """Test multiply with incorrect positive result""" + self.assertEqual(self.calc.multiply(2, 3), 5) # Should be 6 + + def test_multiply_wrong_negative(self): + """Test multiply with incorrect negative result""" + self.assertEqual(self.calc.multiply(4, -2), -7) # Should be -8 + + # Tests for subtract() + def test_subtract_positive_integers(self): + """Test subtract with positive integers""" + self.assertEqual(self.calc.subtract(5, 3), 2) + + def test_subtract_negative_integers(self): + """Test subtract with negative integers""" + self.assertEqual(self.calc.subtract(-5, -2), -3) + + def test_subtract_mixed(self): + """Test subtract with positive and negative integers""" + self.assertEqual(self.calc.subtract(5, -2), 7) + + def test_subtract_floats(self): + """Test subtract with floating-point numbers""" + self.assertEqual(self.calc.subtract(3.5, 1.5), 2.0) + + def test_subtract_wrong(self): + """Test subtract with incorrect result""" + self.assertEqual(self.calc.subtract(5, 3), 3) # Should be 2 + + # Tests for divide() + def test_divide_positive_integers(self): + """Test divide with positive integers""" + self.assertEqual(self.calc.divide(6, 2), 3) + + def test_divide_negative_integers(self): + """Test divide with negative integers""" + self.assertEqual(self.calc.divide(-6, -2), 3) + + def test_divide_mixed(self): + """Test divide with positive and negative integers""" + self.assertEqual(self.calc.divide(8, -2), -4) + + def test_divide_floats(self): + """Test divide with floating-point numbers""" + self.assertEqual(self.calc.divide(4.5, 1.5), 3.0) + + def test_divide_by_zero(self): + """Test divide by zero raises ValueError""" + with self.assertRaises(ValueError): + self.calc.divide(5, 0) + + def test_divide_wrong(self): + """Test divide with incorrect result""" + self.assertEqual(self.calc.divide(6, 2), 2) # Should be 3 + +if __name__ == '__main__': + unittest.main() \ No newline at end of file