lab coat: test your specimens
DESCRIPTION
My talk from the Testing in Python BoF at PyCon 2011TRANSCRIPT
![Page 1: Lab Coat: Test your specimens](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/1.jpg)
Lab CoatTest your specimens
![Page 2: Lab Coat: Test your specimens](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/2.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/3.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/4.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/5.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/7.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/8.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/9.jpg)
class Human: legs = ['left', 'right']
How to examine a human
human.has.legs
![Page 10: Lab Coat: Test your specimens](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/10.jpg)
class Human: legs = ['left', 'right']
How to examine a human
human.lacks.tail
![Page 11: Lab Coat: Test your specimens](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/11.jpg)
class Human: legs = ['left', 'right']
How to examine a human
human.lacks(4).legs
![Page 12: Lab Coat: Test your specimens](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/14.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/15.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/16.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/17.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/18.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/19.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/20.jpg)
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://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/21.jpg)
https://github.com/gulopine/labcoat
![Page 22: Lab Coat: Test your specimens](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/22.jpg)
https://github.com/gulopine/labcoat
A week oldNo docs
No setup.py
![Page 23: Lab Coat: Test your specimens](https://reader033.vdocument.in/reader033/viewer/2022051014/54c787934a7959d2718b457e/html5/thumbnails/23.jpg)
https://github.com/gulopine/labcoat
A week oldNo docs
No setup.pyNo tests