Top Banner
Lab Coat Test your specimens
23

Lab Coat: Test your specimens

Jan 27, 2015

Download

Technology

Marty Alchin

My talk from the Testing in Python BoF at PyCon 2011
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lab Coat: Test your specimens

Lab CoatTest your specimens

Page 2: Lab Coat: Test your specimens

class Human: legs = ['left', 'right']

class Dog: legs = ['lf', 'rf', 'lr', 'rr'] tail = True fur_color = 'brown'

def bark(self): return 'Woof!'

First some species

Page 3: Lab Coat: Test your specimens

Then some observations● Humans have two legs, dogs have four● Dogs have a tail and fur of a specific color● Dogs can bark, humans can�t (for our purposes)● Humans are actually pretty boring

Page 4: Lab Coat: Test your specimens

class Dog: legs = ['lf', 'rf', 'lr', 'rr'] tail = True fur_color = 'brown'

def bark(self): return 'Woof!'

How to examine a dog

dog.has.tail

Page 5: Lab Coat: Test your specimens

class Dog: legs = ['lf', 'rf', 'lr', 'rr'] tail = True fur_color = 'brown'

def bark(self): return 'Woof!'

How to examine a dog

dog.has.legs

Page 6: Lab Coat: Test your specimens

class Dog: legs = ['lf', 'rf', 'lr', 'rr'] tail = True fur_color = 'brown'

def bark(self): return 'Woof!'

How to examine a dog

dog.has(4).legs

Page 7: Lab Coat: Test your specimens

class Dog: legs = ['lf', 'rf', 'lr', 'rr'] tail = True fur_color = 'brown'

def bark(self): return 'Woof!'

dog.can.bark()

How to examine a dog

Page 8: Lab Coat: Test your specimens

class Dog: legs = ['lf', 'rf', 'lr', 'rr'] tail = True fur_color = 'brown'

def bark(self): return 'Woof!'

dog.s.fur_color == 'brown'

How to examine a dog

Page 9: Lab Coat: Test your specimens

class Human: legs = ['left', 'right']

How to examine a human

human.has.legs

Page 10: Lab Coat: Test your specimens

class Human: legs = ['left', 'right']

How to examine a human

human.lacks.tail

Page 11: Lab Coat: Test your specimens

class Human: legs = ['left', 'right']

How to examine a human

human.lacks(4).legs

Page 12: Lab Coat: Test your specimens

class Human: legs = ['left', 'right']

How to examine a human

human.can_not.bark()(for today�s purposes)

Page 13: Lab Coat: Test your specimens

class Furniture: legs = ['a', 'b', 'c', 'd']

class Dog: def hump(self, obj): ...

How to examine interaction

dog.can.hump(furniture)

Page 14: Lab Coat: Test your specimens

class Furniture: legs = ['a', 'b', 'c', 'd']

class Dog: def hump(self, obj): ...

How to examine interaction

dog.can.hump(human)

Page 15: Lab Coat: Test your specimens

class BadDog(Exception): pass

class Dog: def hump(self, obj): if isinstance(obj, Human): raise BadDog('Down boy!')

How to examine interaction

dog.can_not.hump(human)

Page 16: Lab Coat: Test your specimens

class BadDog(Exception): pass

class Dog: def hump(self, obj): if isinstance(obj, Human): raise BadDog('Down boy!')

How to examine interaction

dog.can_not.hump(human) \.because(BadDog)

Page 17: Lab Coat: Test your specimens

from labcoat import Specimen

with Specimen(Dog) as rover: rover.has.tail rover.has(4).legs rover.can.bark()

with Specimen(Furniture) as couch: rover.can.hump(couch)

with Specimen(Human) as owner: rover.can_not.hump(owner).because(BadDog)

Obtaining specimens

Page 18: Lab Coat: Test your specimens

black_lab = Specimen(Dog)black_lab.fur_color = 'black'

with black_lab as rover: rover.s.fur_color != 'brown' rover.s.fur_color == 'black'

# Dye the dog's fur brown rover.s.fur_color = 'brown'

with black_lab as rover: rover.s.fur_color == 'black'

Specimen initialization

Page 19: Lab Coat: Test your specimens

specimen.s.attribute <|<=|==|!=|>|>= 'value'

specimen.has.attributespecimen.has(num).attributesspecimen.has(num).or_more.attributesspecimen.has(num).or_less.attributes

specimen.lacks.attributespecimen.lacks(num).attributesspecimen.lacks(num).or_more.attributesspecimen.lacks(num).or_less.attributes

Attribute tests

Page 20: Lab Coat: Test your specimens

specimen.please.act()

specimen.can.act()specimen.can_not.act()

specimen.can_not.act().because(Exception)

Method tests

Page 21: Lab Coat: Test your specimens

https://github.com/gulopine/labcoat

Page 22: Lab Coat: Test your specimens

https://github.com/gulopine/labcoat

A week oldNo docs

No setup.py

Page 23: Lab Coat: Test your specimens

https://github.com/gulopine/labcoat

A week oldNo docs

No setup.pyNo tests