Question
Please help me with this python unit test, creating unit test method for loc_from_tuple (def test_loc_from_tuple1 and def test_loc_from_tuple2), resolving the AssertionError. tests.py import unittest
Please help me with this python unit test, creating unit test method for loc_from_tuple (def test_loc_from_tuple1 and def test_loc_from_tuple2), resolving the AssertionError.
tests.py
import unittest
from shapes import Point
def point_data(point):
return(point.x, point.y)
class PointTests(unittest.TestCase):
def test_create_point_no_data(self):
expected=(0, 0)
point=Point()
self.assertEqual(point_data(point),expected)
def test_create_point_with_data(self):
expected=(2, 3)
point=Point(2, 3)
self.assertEqual(point_data(point),expected)
def test_point_modification(self):
expected=(-1, 5)
point = Point()
point.x, point.y = -1, 5
self.assertEqual(point_data(point),expected)
def test_point_iterable(self):
expected=(2, 3)
point=Point(2, 3)
x, y=point
self.assertEqual((x,y),expected)
def test_magnitude(self):
expected=3.605551275463989
point=Point(2, 3)
x,y=point
self.assertEqual(point.magnitude,expected)
def test_magnitude_changes(self):
expected=5
point=Point(-1, 6)
point.x, point.y=3, 4
self.assertEqual(point.magnitude,expected)
def test_distance(self):
expected=5
point1=Point(2, 3)
point2=Point(5, 7)
self.assertEqual(point1.distance(point2),expected)
def test_point_addition(self):
expected1=(2, 3)
expected2=(4, 5)
expected3=(6, 8)
point1=Point(2, 3)
point2=Point(4, 5)
point3=point1+point2
self.assertEqual(point_data(point1),expected1)
self.assertEqual(point_data(point2),expected2)
self.assertEqual(point_data(point3),expected3)
def test_point_str(self):
expected="Point at (2, 3)"
point=Point(2, 3)
self.assertEqual(str(point),expected)
def test_point_repr(self):
expected="Point(x=2, y=3)"
point=Point(2, 3)
self.assertEqual(repr(point),expected)
def test_mul_function(self):
expected="Point(x=8, y=10)"
point=Point(4, 5)
self.assertEqual(repr(2*point), expected)
def test_mul_scalar(self):
expected="Point(x=6, y=9)"
point=Point(2, 3)
self.assertEqual(repr(point*3), expected)
def test_from_tuple1(self):
expected="Point(x=2, y=3)"
point=Point(2, 3)
self.assertEqual(repr(Point.from_tuple(point)),expected)
def test_from_tuple2(self):
expected="Point(x=0, y=0)"
self.assertEqual(repr(Point.from_tuple()),expected)
def test_loc_from_tuple1(self):
expected=Point(5, 6)
point1=Point(3, 4)
point2=point1.loc_from_tuple((5, 6))
self.assertEqual(point2,expected)
def test_loc_from_tuple2(self):
expected=Point(0, 0)
point=Point(3, 4)
self.assertEqual(point.loc_from_tuple(),expected)
if __name__=="__main__":
unittest.main(exit=False)
shapes.py
import math
class Point:
def __init__(self,x=0,y=0):
self.x = x
self.y = y
def __iter__(self):
yield self.x
yield self.y
def __add__(self,other):
return Point(self.x+other.x,self.y+other.y)
def __mul__(self,n):
return Point(self.x*n,self.y*n)
def __rmul__(self,n):
return Point(n*self.x,n*self.y)
@classmethod
def from_tuple (cls, self=(0, 0)):
return cls(*self)
def loc_from_tuple(self,t=(0, 0)):
self.x=t[0]
self.y=t[1]
def __str__(self):
return "Point at ({0}, {1})".format(self.x,self.y)
def __repr__(self):
return"Point(x={0}, y={1})".format(self.x,self.y)
@property
def magnitude(self):
return math.sqrt(self.x**2+self.y**2)
def distance (self, other):
return math.sqrt((self.x-other.x)**2+(self.y-other.y)**2)
def shift(p1, p2):
mx = p1.x+p2.x
my = p1.y+p2.y
return Point(mx,my)
1 import unittest 2 from shapes import Point 3 def point_data (point): return (point.x, point.y) 5 Eclass PointTests (unittest.TestCase): def test create point no data (self): expected- (0, 0) point Point () self.assertEqual (point data (point),expected) 10 def test create point with data (self): expected= (2, 3) point Point (2, 3) self.assertEqual (point data (point),expected) 12 13 14 15 16 17 18 19 20 21 def test point modification (self): expected (-1, 5) point -Point () point . x, point. y =-1 , 5 self.assertEqual (point data (point),expected) def test point iterable (self) expected= (2, 3) point Point (2, 3) x, y=point self.assertEqual ( (x,y),expected) 23 24 25 26 27 28 29 30 31 32 def test magnitude (self) expected 3.605551275463989 point Point (2, 3) x,y-point self.assertEqual (point.magnitude, expected) def test magmitude changes (self): expected- 5 point Point(-1, 6) point , x , point . y=3 , 4 self.assertEqual (point.magnitude, expected) def test distance (self) 34 35 36 37 38 expected- 5 pointl Point (2, 3) point2 Point (5, 7) self.assertEqual (point1.distance (point2),expected)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started