Unittest
Unittest
radius, having follwing restrictions. a. radius must be numeric value, if not raise
type error with error message "radius must be number".
b. radius must be between 0 to 1000 inclusive on both sides, if not raise the value
error with error message "radius must be between 0 and 1000 inclusive"
c. Define a class method area and circumference which must return values rounded
off to 2 decimals.
# Define below the class 'Circle' and it's methods with proper doctests.
class Circle:
def test_creating_circle_with_numeric_radius(self):
# Define a circle 'c1' with radius 2.5 and check if
# the value of c1.radius equal to 2.5 or not
c1=Circle(2.5)
self.assertEqual(c1.radius,2.5)
def test_creating_circle_with_negative_radius(self):
# Try Defining a circle 'c' with radius -2.5 and see
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive"
with self.assertRaises(ValueError) as e:
c=Circle(-2.5)
self.assertEqual(str(e.exception),"radius must be between 0 and 1000
inclusive")
def test_creating_circle_with_greaterthan_radius(self):
# Try Defining a circle 'c' with radius 1000.1 and see
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive"
with self.assertRaises(ValueError) as e:
c=Circle(1000.1)
self.assertEqual(str(e.exception),"radius must be between 0 and 1000
inclusive")
def test_creating_circle_with_nonnumeric_radius(self):
# Try Defining a circle 'c' with radius 'hello' and see
# if it raises a TypeError with the message
# "radius must be a number"
with self.assertRaises(TypeError) as e:
c=Circle("hello")
self.assertEqual(str(e.exception),"radius must be a number")
********
3 rd
*******
Define a class Circle with method init which initializes a cicle with attribute
radius, having follwing restrictions.
radius must be numeric value, if not raise type error with error message "radius
must be number".
radius must be between 0 to 1000 inclusive on both sides, if not raise the value
error with error message "radius must be between 0 and 1000 inclusive"
Define a class method area and circumference which must return values rounded off
to 2 decimals.
Complete the definition of class TestingCircleCircumference which tests the
behaviour of circumference method as specification below.
class Circle:
def circumference(self):
# Define the circumference functionality below
return round(2*math.pi*self.radius,2)
class TestCircleCircumference(unittest.TestCase):
def test_circlecircum_with_random_numeric_radius(self):
# Define a circle 'c1' with radius 2.5 and check if
# it's circumference is 15.71
c1 = Circle(2.5)
self.assertEqual(c1.circumference(),15.71)
def test_circlecircum_with_min_radius(self):
# Define a circle 'c2' with radius 0 and check if
# it's circumference is 0.
c2 = Circle(0)
self.assertEqual(c2.circumference(),0)
def test_circlecircum_with_max_radius(self):
# Define a circle 'c3' with radius 1000 and check if
# it's circumference is 6283.19.
c3 = Circle(1000)
self.assertEqual(c3.circumference(),6283.19)
if __name__ == '__main__':
runner = unittest.TextTestRunner(fptr)
unittest.main(testRunner=runner, exit=False)
fptr.close()
pass_count = pass_count[0]
print(str(pass_count))
doc1 =
inspect.getsource(TestCircleCircumference.test_circlecircum_with_random_numeric_rad
ius)
doc2 =
inspect.getsource(TestCircleCircumference.test_circlecircum_with_min_radius)
doc3 =
inspect.getsource(TestCircleCircumference.test_circlecircum_with_max_radius)
print(str(assert1_count))
print(str(assert1_count))
if __name__ == '__main__':
runner = unittest.TextTestRunner(fptr)
unittest.main(testRunner=runner, exit=False)
fptr.close()
pass_count = pass_count[0]
print(str(pass_count))
doc1 =
inspect.getsource(TestCircleCreation.test_creating_circle_with_numeric_radius)
doc2 =
inspect.getsource(TestCircleCreation.test_creating_circle_with_negative_radius)
doc3 =
inspect.getsource(TestCircleCreation.test_creating_circle_with_greaterthan_radius)
doc4 =
inspect.getsource(TestCircleCreation.test_creating_circle_with_nonnumeric_radius)
print(str(assert1_count))
print(str(assert1_count), str(assert2_count))
print(str(assert1_count), str(assert2_count))
print(str(assert1_count), str(assert2_count))