21. Unit testing: nose
class A(object):
def __init__(self):
self.value = "Some Value"
def return_true(self):
return True save it in a.py
def raise_exc(self, val):
raise KeyError(val)
22. Unit testing: nose
from a import A
from nose.tools import assert_equal
from nose.tools import assert_not_equal
from nose.tools import assert_raises
from nose.tools import raises
class TestA(object):
@classmethod
def setup_class(klass):
"""This method is run once for each class before any tests are run"""
@classmethod
def teardown_class(klass):
"""This method is run once for each class _after_ all tests are run"""
def setUp(self):
"""This method is run once before _each_ test method is executed"""
def teardown(self):
"""This method is run once after _each_ test method is executed"""
.... continue ....
23. Unit testing: nose
def test_init(self):
a = A()
assert_equal(a.value, "Some Value")
assert_not_equal(a.value, "Incorrect Value")
def test_return_true(self):
a = A()
assert_equal(a.return_true(), True)
assert_not_equal(a.return_true(), False)
def test_raise_exc(self):
a = A()
assert_raises(KeyError, a.raise_exc, "A value")
@raises(KeyError)
def test_raise_exc_with_decorator(self):
a = A()
a.raise_exc("A message")
save it in tests/testa.py
25. Unit testing: nose
$ nosetests -v tests/
testa.TestA.test_init ... ok
testa.TestA.test_raise_exc ... ok
testa.TestA.test_raise_exc_with_decorator ... ok
testa.TestA.test_return_true ... ok
---------------------------------------------------------
Ran 4 tests in 0.002s
OK
26. Unit testing: Bonus: code coverage
$ pip install coverage
$ nosetests --with-coverage
....
Name Stmts Miss Cover Missing
-------------------------------------
a 8 0 100%
-------------------------------------
Ran 4 tests in 0.006s OK
28. Python eggs: basic setup.py
from setuptools import setup
setup(
name = "example",
version = "1.0",
description = "An example package",
author='Andres J. Diaz'
)
29. Python eggs: basic setup.py
from setuptools import setup, find_packages
setup(
name = "example",
version = "1.0",
description = "An example package",
author='Andres J. Diaz',
packages=find_packages()
)
30. Python eggs: complex setup.py
import re
from setuptools import setup, find_packages
from os import path
def parse_requirements(file_name):
requirements = []
for line in open(file_name, 'r').read().split('n'):
if re.match(r'(s*#)|(s*$)', line):
continue
if re.match(r's*-es+', line):
requirements.append(re.sub(r's*-es+.*#egg=(.*)$', r'1', line))
elif re.match(r's*-fs+', line):
pass
else:
requirements.append(line)
return requirements
.... continue ....
31. Python eggs: complex setup.py
def parse_dependency_links(file_name):
dependency_links = []
for line in open(file_name, 'r').read().split('n'):
if re.match(r's*-[ef]s+', line):
dependency_links.append(re.sub(r's*-[ef]s+', '', line))
return dependency_links
def get_file_contents(filename):
fd = file(path.join(path.dirname(__file__), filename), "r")
content = fd.read()
fd.close()
return content
.... continue ....
32. Python eggs: complex setup.py
setup(
name = "mico",
version = "0.1",
description = "A monkey driven cloud management",
long_description=get_file_contents("README.rst"),
author='Andres J. Diaz',
author_email='ajdiaz@connectical.com',
url='http://ajdiaz.github.com/mico',
packages=find_packages(),
install_requires = parse_requirements('requirements.txt'),
dependency_links = parse_dependency_links('requirements.txt'),
entry_points={
'console_scripts': [
'mico = mico.scripts.cmdline:main',
]
},
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU General Public License (GPL)',
'Operating System :: OS Independent',
'Programming Language :: Python',
],
)
33. Python eggs: complex setup.py
setup(
name = "mico",
version = "0.1",
description = "A monkey driven cloud management",
long_description=get_file_contents("README.rst"),
author='Andres J. Diaz',
author_email='ajdiaz@connectical.com',
url='http://ajdiaz.github.com/mico',
packages=find_packages(),
install_requires = parse_requirements('requirements.txt'),
dependency_links = parse_dependency_links('requirements.txt'),
entry_points={
'console_scripts': [
'mico = mico.scripts.cmdline:main',
]
},
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU General Public License (GPL)',
'Operating System :: OS Independent',
'Programming Language :: Python',
],
)