def main(): # Create a circle and test it circle1 = Circle(5) print(circle1.area()) print(circle1.circumference()) circle2 = Circle(10) print(circle2.area()) print(circle2.circumference())
class TestCircle(unittest.TestCase): def setUp(self): self.circle = Circle(29) # Return pi def test_area(self): self.assertEqual(2642.079421669016, self.circle.area())
def test_circle_zero_radius(): centre = (1, 1) radius = 0 circle = Circle(centre, radius) assert circle.perimeter() == 0 assert circle.area() == 0
def test_10_set_area(self): c = Circle(10) try: c.area = 11 except AttributeError: caught = 'AttributeError' self.assertEqual(caught, 'AttributeError')
def main(): # Use this to test the constructor c1 = Circle(0, 0, 3) # Use these to test the getters print("c1 center =", c1.getCenter()) print("c1 radius =", c1.getRadius()) # Use these to test the setters c1.setCenter(1, 2) c1.setRadius(5) print() print("Modified c1 center =", c1.getCenter()) print("Modified c1 radius =", c1.getRadius()) # Use this to test the area method print("c1 area =", c1.area()) # Use this to test the __str__ print() print("Modified Circle c1:") print(c1)
def test_zero_radius(self): c = Circle(0) perimeter = c.perimeter() area = c.area() exp_perimeter = 0 exp_area = 0 self.assertEqual(perimeter, exp_perimeter) self.assertEqual(area, exp_area)
def test_positive_radius(self): c = Circle(10) perimeter = c.perimeter() area = c.area() exp_perimeter = 62.83185307179586 exp_area = 314.1592653589793 self.assertEqual(perimeter, exp_perimeter) self.assertEqual(area, exp_area)
def test_set_area(): c = Circle(4) try: c.area = 10 except AttributeError as error: assert error.message == "can't set attribute" else: assert False
def test_diameter_changeable_but_area_not(self): circle = Circle(2) self.assertEqual(circle.diameter, 4) self.assertEqual(circle.area, math.pi * 4) circle.diameter = 3 self.assertEqual(circle.radius, 1.5) with self.assertRaises(AttributeError): circle.area = 3
def test_circle_area(): c = Circle(2) assert c.area == 12.566370614359172 with pytest.raises(Exception) as excinfo: c = Circle(2) c.area = 16 assert str(excinfo.value) == "can't set attribute"
def test_circle_change_area_error(): msg = "" c = Circle(2) try: c.area = 42 except AttributeError as e: msg = e.args[0] assert msg == "can't set attribute"
class TestCircle(unittest.TestCase): def setUp(self): self.tmp = Circle(1, 2, 3) self.tmp2 = Circle(0, 0, 0) def test_init(self): self.assertEqual(self.tmp.pt, Point(1, 2)) self.assertEqual(self.tmp.pt.x, 1) self.assertEqual(self.tmp.pt.y, 2) self.assertEqual(self.tmp.radius, 3) with self.assertRaises(ValueError): Circle(0, 0, -1) def test_repr(self): self.assertEqual(repr(self.tmp), "Circle(1, 2, 3)") def test_eq(self): self.assertFalse(self.tmp == Circle(1, 2, 4)) self.assertTrue(self.tmp == Circle(1.0, 2.0, 3.0)) self.assertTrue(self.tmp == Circle(1, 2, 3)) def test_ne(self): self.assertTrue(self.tmp != Circle(1, 2, 4)) self.assertFalse(self.tmp != Circle(1.0, 2.0, 3.0)) self.assertFalse(self.tmp != Circle(1, 2, 3)) def test_area(self): self.assertEqual(self.tmp.area(), math.pi * pow(3, 2)) self.assertEqual(self.tmp2.area(), 0) def test_move(self): self.tmp.move(1, 2) self.assertEqual(self.tmp, Circle(2, 4, 3)) self.tmp2.move(1, 2) self.assertEqual(self.tmp2, Circle(1, 2, 0)) def test_cover(self): self.assertEqual(self.tmp.cover(self.tmp2), Circle(1, 2, 3)) self.assertEqual(self.tmp2.cover(self.tmp), Circle(1, 2, 3)) self.assertEqual( Circle(0, 0, 2).cover(Circle(4, 0, 2)), Circle(2, 0, 4)) def tearDown(self): pass
def test_area(): c = Circle(2) assert c.area == 4 * pi with pytest.raises(AttributeError) as e: c.area = 4 assert str(e.value) == 'can\'t set attribute'
def test_set_area(): ''' Test 7: Testing AttributeError ''' #pylint: disable=C0103 c = Circle(7) with pytest.raises(AttributeError): c.area = 10
def main(args): radius = int(args[1]) print("radius: ", radius) print("Creating circle of Radius = %s" % (radius)) c1 = Circle(radius) print("Perimeter: ", c1.perimeter()) print("Area: ", c1.area())
def test_area(): c = Circle(2) assert c.area == 12.566370614359172 s = Sphere(2) assert s.area == 50.26548245743669 s = Sphere(4) assert round(s.area, 5) == 201.06193 with pytest.raises(AttributeError): c.area = 42 s.area = 42
def test_area(): my_circle = Circle(5) assert my_circle.area == (pi * 5 ** 2) # Area should not be able to be set try: my_circle.area = 10 except AttributeError: pass else: assert False
def test_area(): c = Circle(10) assert (c.area == 314.1592653589793) # Verify area isn't settable try: c2 = Circle(2) c2.area = 42 except AttributeError: pass else: assert (False)
def test_set_area(): """ tests the area of a circle then assigns a new area and re-tests the new area. This uses the isclose module to test the relative tolerance of the radius and diameter, separately, to one one-hundredth :return: None """ c = Circle(2) assert isclose(c.area, (pi * 2**2), rel_tol=.01) c.area = 500 assert isclose(c.area, 500, rel_tol=.01)
def test_change_values_area(): """ tests changing the assigned radius of a circle by assigning a new area for the circle and testing the resultant radius and diameter with the math, isclose module. This isclose module tests the relative tolerance of the radius and diameter, separately, to one one-hundredth :return: None """ c = Circle(13) c.area = 6 assert isclose(c.radius, 1.38, rel_tol=.01) assert isclose(c.diameter, c.radius * 2, rel_tol=.01)
def test_diameter_changeable_but_area_not(self): """ The value of diameter should *also* be changable, and should update the radius automatically, but the area should raise an error when a caller attempts to change the value """ circle = Circle(2) self.assertEqual(circle.diameter, 4) self.assertEqual(circle.area, math.pi * 4) circle.diameter = 3 self.assertEqual(circle.radius, 1.5) with self.assertRaises(AttributeError): circle.area = 3
def test_circle_initialization_str_repr_and_properties(self): c1 = Circle(10) c2 = Circle.from_diameter(40) assert type(c2) == Circle self.assertEqual(c1.radius, 10.0) self.assertEqual(c1.diameter, 20.0) self.assertEqual(c2.radius, 20.0) self.assertEqual(c2.diameter, 40.0) c1.radius = 30 self.assertEqual(c1.diameter, 60.0) c2.diameter = 30 self.assertEqual(c2.radius, 15.0) self.assertEqual(str(c1), "Circle with radius: 30.000") self.assertEqual(repr(c1), "Circle(30.0)") self.assertEqual(round(c1.area), 2827) with pytest.raises(AttributeError) as error: c1.area = 50 assert "AttributeError: can't set attribute" in error
class TestCircle(unittest.TestCase): def setUp(self): self.c1 = Circle(1, 1, 1) self.c2 = Circle(3, 1, 1) def test_repr(self): self.assertEqual(repr(self.c1), "Circle(1, 1, 1)") self.assertEqual(repr(self.c2), "Circle(3, 1, 1)") def test_eq(self): self.assertTrue(self.c1 == Circle(1, 1, 1)) self.assertFalse(self.c2 == Circle(1, 3, 1)) with self.assertRaises(Exception) as context: self.c1 == 1 self.assertTrue(isinstance(context.exception, TypeError)) def test_ne(self): self.assertFalse(self.c1 != Circle(1, 1, 1)) self.assertTrue(self.c2 != Circle(1, 3, 1)) with self.assertRaises(Exception) as context: self.c1 != 1 self.assertTrue(isinstance(context.exception, TypeError)) def test_area(self): self.assertEqual(self.c1.area(), math.pi) self.assertEqual(Circle(1, 1, 5).area(), 25 * math.pi) def test_move(self): self.assertEqual(self.c1.move(2, 0), self.c2) def test_cover(self): self.assertEqual(self.c1.cover(self.c2), Circle(2, 1, 2)) with self.assertRaises(Exception) as context: self.c1.cover(2) self.assertTrue(isinstance(context.exception, TypeError)) def tearDown(self): pass
def test_set_area(): c = Circle(10) with pytest.raises(AttributeError): c.area = 100
# Tutorial from circle import Circle print 'Circuitous version', Circle.version c = Circle(10) print 'A circle of radius', c.radius print 'has an area of', c.area() print
def test_set_area(): c = Circle(4) with pytest.raises(AttributeError): c.area = 44
def test_circle_area(): my_circle = Circle(4) assert my_circle.area == 50.26548245743669 #Test that area is read-only with pytest.raises(AttributeError): my_circle.area = 10
def test_areaset(): c = Circle(2) with pytest.raises(AttributeError): c.area = 4*pi
def test_no_set_area(): my_circle = Circle(3) with pytest.raises(AttributeError): my_circle.area = 20
from circle import Circle c = Circle(4) c.set_center([0, 0]) c.set_color("Yellow") print "Area of %s %sis %f" % (c.get_color(), c.name, c.area())
def test_no_set_area(): c = Circle(4.52) with pytest.raises(AttributeError): c.area = 5.67
def test_area_of_circle(): circle = Circle(2) assert math.isclose(circle.area, 12.566370, rel_tol=1e-7) with pytest.raises(AttributeError): circle.area = 42
from circle import Circle bbd = 25.1 # NameError: name 'bbd_to_radius' is not defined c = Circle(bbd_to_radius(bbd)) print 'A circle with a bbd of 25.1' print 'has a radius of',c.radius print 'and an area of',c.area() print
def test_area(): c = Circle(4) assert c.area == math.pi * 16 with pytest.raises(AttributeError): c.area = 1
def test_step4(): c = Circle(2) assert c.area == pi * 2**2 with pytest.raises(AttributeError): c.area = 10
# Tutorial from circle import Circle print "Citrus version", Circle.version c = Circle(10) print "A circle of radius", c.radius print "has an area of", c.area() print
def test_area(self): cir = Circle(3) self.assertEqual(28.27, cir.area())
print "the diameter:", c.diameter print "the area:", c.area print print "setting the diameter to 6:" c.diameter = 6 print "the radius:", c.radius print "the diameter:", c.diameter print "the area:", c.area print print "trying to delete the diameter" try: del c.diameter except Exception as err: print "Whoops:", err print print "trying to set the area" try: c.area = 40.0 except Exception as err: print "Whoops:", err c1 = Circle(2) c2 = Circle(4) print print "adding two circles together" print c1 + c2
def test_step4(self): c = Circle(2) self.assertAlmostEqual(c.area, 2**2 * pi) with self.assertRaises(AttributeError): c.area = 42 print("The user cannot set the area")
from circle import Circle c = Circle(4) c.set_center([0, 0]) c.set_color("Yellow") c.name = "Sun" print "Area of %s is %f" % (c.name, c.area())