mardi 10 mars 2020

Unit Testing using Nose in Python

Define a class 'Circle' with the following specifications:

  • Define the class method 'init', which initializes a circle with attribute 'radius', having the following restrictions:
    • 'radius' must be a numeric value. If not, raise TypeError with error message "radius must be a number".
    • 'radius' must be between 0 and 1000, inclusive on both sides. If not, raise ValueError with error message "radius must be between 0 and 1000 inclusive".
  • Define the class method 'area', which computes the area of the circle, and returns the value rounded off to 2 decimals. Hint: Make use of the 'pi' value from the 'math' module.
  • Define the class method 'circumference', which computes the circumference of the circle, and returns the value rounded off to 2 decimals. Hint: Make use of the 'pi' value from the 'math' module.

Define a nose test class 'TestingCircleCreation' which tests the behavior of the '__init__' method with the following tests:

  • Define a nose test method 'test_creating_circle_with_numeric_radius', which creates a circle with radius 2.5, and check if its radius value is 2.5.
  • Define a nose test method 'test_creating_circle_with_negative_radius', which checks if the ValueError exception is raised with the error message "radius must be between 0 and 1000 inclusive", while creating a circle of radius -2.5. Hint: Use assert_raises and with.
  • Define a nose test method 'test_creating_circle_with_greaterthan_radius', which checks if the ValueError exception is raised with the error message "radius must be between 0 and 1000 inclusive" , while creating circle of radius 1000.1 . Hint: Use assert_raises and with
  • Define a nose test method 'test_creating_circle_with_nonnumeric_radius', which checks if the TypeError exception is raised with the error message "radius must be a number", while creating circle of radius 'hello' . Hint: Use assert_raises and with.
class Circle:

def __init__(self, radius):
    # Define the initialization method below
    self.radius=radius
    if not isinstance(self.radius,(int,float)):
        raise TypeError("radius must be a number")
    elif(self.radius>1000 or self.radius<0):
        raise ValueError("radius must be between 0 and 1000 inclusive")
    else:
        pass
def area(self):
    # Define the area functionality below
    return math.pi*(self.radius**2)
def circumference(self):
    return 2*math.pi*self.radius
    # Define the circumference functionality below
class TestingCircleCreation:
def test_creating_circle_with_numeric_radius(self):
    c1=Circle(2.5)
    assert c1.radius==2.5

def test_creating_circle_with_negative_radius(self):
    c1=Circle(2.5)
    assert c1.radius==2.5
    with assert_raises(ValueError):
        Circle(-2.5)


def test_creating_circle_with_greaterthan_radius(self):
    c1=Circle(2.5)
    assert c1.radius==2.5
    with assert_raises(ValueError):
        Circle(1000.1)

def test_creating_circle_with_nonnumeric_radius(self):
    c1=Circle(2.5)
    assert c1.radius==2.5
    with assert_raises(TypeError):
        Circle('hello')

Aucun commentaire:

Enregistrer un commentaire