def test_hunger_can_not_be_initialized_to_non_int(bad_type): """ When a Pet() is created, the hunger value must be an integer. This is an infinite search space, so just settle for a few basic types for bad data type checking. """ if isinstance(bad_type, (str, float)): # These types *could* be cast by the user as I do here. This # is not an error, and so this should not throw/assert. pet = example.Pet("Odie", hunger=int(bad_type)) # But for all bad_types, using it directly should be an error. with pytest.raises(TypeError): pet = example.Pet("Odie", hunger=bad_type)
def test_hunger_is_a_read_only_attribute(): """ A Pet() object should have an attribute called hunger, and it should be read only. """ pet = example.Pet("Odie") assert hasattr(pet, "hunger") with pytest.raises(AttributeError): pet.hunger = 5
def test_pet_gets_hungry_after_walk(): """ When a Pet() goes for a walk, it's hunger should increment by one. """ pet = example.Pet("Odie") pet.walk() assert pet.hunger == 1 pet.walk() assert pet.hunger == 2
def test_pet_gets_full_after_feeding(): """ When a Pet() is fed, then the hunger should be reset to zero. """ pet = example.Pet("Odie") pet.walk() assert pet.hunger == 1 pet.walk() assert pet.hunger == 2 pet.feed() assert pet.hunger == 0
import example # p = example.Pet("Molly") p = example.Pet('Polly', 'parrot') print p print p.name() p.setName("Charly") print p.name() p.age = 2 print p.__dict__ ### Derived Class b = Dog('aaa') example.dog_bark(b)
import example p = example.Pet('Molly') print(p) print(p.getName()) p.setName('Charly') print(p.getName())
#!/usr/bin/env python import example momo = example.Pet("momo") vec = momo.getvec() print(vec) momo.pushvec(1) momo.pushvec(2) momo.pushvec(4) momo.pushvec(8) vec = momo.getvec() print(vec) mat = momo.getmat() print(mat)
def test_pet_requires_name(): """ A Pet cannot be created without a name. """ with pytest.raises(TypeError): example.Pet()
def test_pet_name_is_read_only(): pet = example.Pet("Odie") with pytest.raises(AttributeError): pet.name = "Garfield"
def test_pet_name_can_be_fetched(): """ A Pet's name can be fetched with the name attribute. """ pet = example.Pet("Odie") assert pet.name == "Odie"
assert add(2, 3) == 5 def test_pet(): my_dog = Pet('Pluto', 5) assert my_dog.get_name() == 'Pluto' assert my_dog.get_hunger() == 5 my_dog.go_for_a_walk() assert my_dog.get_hunger() == 6 def taint(x): return x a = 1 b = 2 a = taint(a) b = taint(b) example.add(a, b) print(example) # have to do it like this because of some bug my_dog = example.Pet('Pluto', 5) print(my_dog.get_name()) print(my_dog.get_hunger()) my_dog.go_for_a_walk() print(my_dog.get_hunger())
import example print(example.add(2235, 5214)) my_dog = example.Pet("Pluto", 7) print(my_dog.get_name()) my_dog.get_hunger() my_dog.go_for_a_walk() my_dog.go_for_a_walk() print(my_dog.get_hunger())
def test_hunger_can_be_initialized_to_int(): """ When a Pet() is created, the hunger value may be initialized. """ pet = example.Pet("Odie", hunger=3) assert pet.hunger == 3
def test_hunger_defaults_to_zero(): """ When a Pet() is created, it's hunger should be zero by default. """ pet = example.Pet("Odie") assert pet.hunger == 0