Skip to content
Snippets Groups Projects
Commit c8790a2d authored by muoimeo's avatar muoimeo
Browse files

Add lab 11 works

parent a3ebf064
No related branches found
No related tags found
No related merge requests found
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
lab11/ptest/__MACOSX/._uwe_logo.png

176 B

# 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
lab11/ptest/uwe_logo.png

23.2 KiB

%% Cell type:code id: tags:
``` python
# test_sum_list
def test_sum():
assert sum([1, 2, 3]) == 6, "Should be 6"
if __name__ == "__main__":
test_sum()
print("Everything passed")
```
%% Output
Everything passed
%% Cell type:code id: tags:
``` python
# test_sum_tuple
def test_sum():
assert sum([1, 2, 3]) == 6, "Should be 6"
def test_sum_tuple():
assert sum((1, 2, 2)) == 6, "Should be 6"
if __name__ == "__main__":
test_sum()
test_sum_tuple()
print("Everything passed")
```
%% Output
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[2], line 11
9 if __name__ == "__main__":
10 test_sum()
---> 11 test_sum_tuple()
12 print("Everything passed")
Cell In[2], line 7, in test_sum_tuple()
6 def test_sum_tuple():
----> 7 assert sum((1, 2, 2)) == 6, "Should be 6"
AssertionError: Should be 6
def increment(x):
return x + 1
def decrement(x):
return x - 1
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()
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment