Available on: http://pokoli.github.io/software-testing
def sum(a, b):
return a + b
self.assertEqual(sum(1, 2), 3)
self.assertIsInstance(sum(1, 2), int)
self.assertNotIsInstance(sum(1.0, 2), int)
self.assertRaises(TypeError, sum('1', 2))
self.assertRaises(TypeError, sum())
import requests
from datetime import date
url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
class CurrencyConverter(object):
def get_ratios_xml(date):
return resquests.get(url)
def update_ratios():
self.parse_data(self.get_ratios_xml(date.today()))
def get_eur_to_usd(self, amount):
if not self.ratios:
self.update_ratios()
return self.ratios['usd'] * amount
from mock import patch
converter = CurrencyConverter()
def get_local_ratios_xml(d):
self.assertEqual(d, date.today)
return open('local.xml').read()
with patch.object(CurrencyConverter, 'get_ratios_xml', new=get_local_ratios_xml):
self.assertEqual(converter.get_eur_to_usd(1), 1.12)
$ coverage report -m
Name Stmts Miss Cover Missing
-------------------------------------------------------
my_program.py 20 4 80% 33-35, 39
my_other_module.py 56 6 89% 17-23
-------------------------------------------------------
TOTAL 76 10
The presentation code is avaiable on