Doggie Day Care Given the TestDog class below, write a Dog class that will pass the tests contained in our test class. Do NOT change anything in our test class! Your Dog class must provide the following methods to adequately pass our tests: * __init__ (takes a string name of dog, int age and Boolean indicating a purebred or not), * is_purebred (returns True or False depending on if the dog is a purebred) * clone (makes a non-mutated copy of the current object) * __str__ returns a string representation of the object * __eq__ returns True or False when comparing two instances of the Dog class import unittest from coding import Dog class TestDog(unittest.TestCase): ''' class to test all the methods of the Dog class imports from TestCase Compares inputs and expected outputs with all SimpleFraction methods: * __init__, * is_purebred, * clone (makes a non-mutated copy of the current object) * __str__, represent the Dog as a string * __eq__, compare current object with another Dog. Are they equal? ''' def test_init(self): d = Dog("Fido", 10, True) self.assertEqual(d.name, "Fido") self.assertEqual(d.age, 10) self.assertTrue(d.is_purebred()) d2 = Dog("Fifi", 11, False) self.assertEqual(d2.name, "Fifi") self.assertEqual(d2.age, 11) self.assertFalse(d2.is_purebred()) def test_bad_init(self): with self.assertRaises(ValueError): d = Dog("Fido", -10, True) def test_is_purebred(self): d = Dog("Fido", 10, True) d2 = Dog("Fifi", 11, False) self.assertTrue(d.is_purebred()) self.assertFalse(d2.is_purebred()) def test_eq(self): d = Dog("Fido", 10, True) d2 = Dog("Fifi", 11, False) d3 = Dog("Fifi", 11, False) self.assertTrue(d2.__eq__(d3)) self.assertFalse(d.__eq__(d2)) self.assertTrue(d2 == d3) self.assertFalse(d == d2) def test_clone(self): d2 = Dog("Fifi", 11, False) d3 = d2.clone() self.assertIsNot(d2,d3) # should NOT be same object! self.assertEqual(d2, d3) # but SHOULD be equal! def test_str(self): d = Dog("Fido", 10, True) d2 = Dog("Fifi", 11, False) msg = "Purebred: Fido 10 years" self.assertEqual(d.__str__(), msg) msg = "NOT Purebred: Fifi 11 years" self.assertEqual(d2.__str__(), msg) def main(): unittest.main(verbosity = 3) main()
Doggie Day Care
Given the TestDog class below, write a Dog class that will pass the tests contained in our test class. Do NOT change anything in our test class!
Your Dog class must provide the following methods to adequately pass our tests:
* __init__ (takes a string name of dog, int age and Boolean indicating a purebred or
not),
* is_purebred (returns True or False depending on if the dog is a purebred) * clone (makes a non-mutated copy of the current object)
* __str__ returns a string representation of the object
* __eq__ returns True or False when comparing two instances of the Dog class
import unittest
from coding import Dog
class TestDog(unittest.TestCase):
''' class to test all the methods of the Dog class
imports from TestCase
Compares inputs and expected outputs with all SimpleFraction methods: * __init__,
* is_purebred,
* clone (makes a non-mutated copy of the current object)
* __str__, represent the Dog as a string
* __eq__, compare current object with another Dog. Are they equal? '''
def test_init(self):
d = Dog("Fido", 10, True)
self.assertEqual(d.name, "Fido")
self.assertEqual(d.age, 10)
self.assertTrue(d.is_purebred())
d2 = Dog("Fifi", 11, False)
self.assertEqual(d2.name, "Fifi")
self.assertEqual(d2.age, 11)
self.assertFalse(d2.is_purebred())
def test_bad_init(self):
with self.assertRaises(ValueError):
d = Dog("Fido", -10, True)
def test_is_purebred(self):
d = Dog("Fido", 10, True)
d2 = Dog("Fifi", 11, False)
self.assertTrue(d.is_purebred())
self.assertFalse(d2.is_purebred())
def test_eq(self):
d = Dog("Fido", 10, True)
d2 = Dog("Fifi", 11, False)
d3 = Dog("Fifi", 11, False)
self.assertTrue(d2.__eq__(d3))
self.assertFalse(d.__eq__(d2))
self.assertTrue(d2 == d3) self.assertFalse(d == d2)
def test_clone(self):
d2 = Dog("Fifi", 11, False)
d3 = d2.clone()
self.assertIsNot(d2,d3) # should NOT be same object! self.assertEqual(d2, d3) # but SHOULD be equal!
def test_str(self):
d = Dog("Fido", 10, True)
d2 = Dog("Fifi", 11, False)
msg = "Purebred: Fido 10 years"
self.assertEqual(d.__str__(), msg)
msg = "NOT Purebred: Fifi 11 years"
self.assertEqual(d2.__str__(), msg)
def main():
unittest.main(verbosity = 3)
main()
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images