You are on page 1of 2

import inspect

import re
import unittest
import math
from nose.tools import assert_raises

# Define class 'Circle' and its methods with proper doctests:


class Circle:

def __init__(self, radius):


# Define initialization method:
if not isinstance(radius, int) and not isinstance(radius, float):
raise TypeError('radius must be a number')
if radius < 0 or radius > 1000:
raise ValueError('radius must be between 0 and 1000 inclusive')
self.radius = radius

def area(self):
# Define area functionality:
return round(math.pi * self.radius * self.radius, 2)

def circumference(self):
# Define circumference functionality:
return round(2*math.pi*self.radius, 2)

# from proj.circle import Circle


from nose.tools import assert_raises
class TestCircleCreation:

def test_creating_circle_with_numeric_radius(self):
# Define a circle 'c1' with radius 2.5, and check if
# the value of c1.radius is equal to 2.5 or not.
c1 = Circle(2.5)
assert c1.radius == 2.5

def test_creating_circle_with_negative_radius(self):
# Define a circle 'c' with radius -2.5, and check
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive".
with assert_raises(ValueError) as e:
c = Circle(-2.5)
assert str(e.exception) == 'radius must be between 0 and 1000 inclusive'

def test_creating_circle_with_greaterthan_radius(self):
# Define a circle 'c' with radius 1000.1, and check
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive".
with assert_raises(ValueError) as e:
c = Circle(1000.1)
assert str(e.exception) == 'radius must be between 0 and 1000 inclusive'

def test_creating_circle_with_nonnumeric_radius(self):
# Define a circle 'c' with radius 'hello' and check
# if it raises a TypeError with the message
# "radius must be a number".
with assert_raises(TypeError) as e:
c = Circle('hello')
assert str(e.exception) == 'radius must be a number'

class TestCircleArea:

def test_circlearea_with_random_numeric_radius(self):
# Define a circle 'c1' with radius 2.5, and check if
# its area is 19.63.
c1 = Circle(2.5)
assert c1.area() == 19.63

def test_circlearea_with_min_radius(self):
# Define a circle 'c2' with radius 0, and check if
# its area is 0.
c2 = Circle(0)
assert c2.area() == 0

def test_circlearea_with_max_radius(self):
# Define a circle 'c3' with radius 1000.1. and check if
# its area is 3141592.65.
c3 = Circle(1000)
assert c3.area() == 3141592.65

class TestCircleCircumference:

def test_circlecircum_with_random_numeric_radius(self):
# Define a circle 'c1' with radius 2.5, and check if
# its circumference is 15.71.
c1 = Circle(2.5)
assert c1.circumference() == 15.71

def test_circlecircum_with_min_radius(self):
# Define a circle 'c2' with radius 0, and check if
# its circumference is 0.
c2 = Circle(0)
assert c2.circumference() == 0

def test_circlecircum_with_max_radius(self):
# Define a circle 'c3' with radius 1000, and check if
# its circumference is 6283.19.
c3 = Circle(1000)
assert c3.circumference() == 6283.19

You might also like