Testing systems is important as much as testing software.
You should start system projects using tools like ansible (more in the next lesson), but in old legacy project it's not always possible (see Test Drive Deployment with python and nosetest from Roberto Polli ).
Nose is a very simple test framework that I love to use for testing infrastructures.
Now run the following command from a shell
# nosetests -vs 03_parsing_test.py
# Test scripts are there ;)
from os import chdir
chdir("../scripts")
# %load 02_nosetests_simple.py
"""Introduction to nosetests - 01
Run this script with
#nosetest -v scriptname.py
"""
def setup():
print("This function is run before the testsuite, while")
def teardown():
print("after all tests")
def test_one():
# just name a function like test_* to execute it!
a = 1
assert a == 1
def test_two():
# This test fails and the backtrace is printed
a = 2
assert a == 1, "Expecting a == 1. Was %r" % a
#
# Let's run it!
#
!nosetests -v 02_nosetests_simple.py
# %load 02_nosetests_full.py
"""An articulated nosetest script using classes
- setup_class() and setup() increase flexibility
"""
import os
import errno
# Import assert_* tools
from nose.tools import *
class TestB(object):
@classmethod
def setup_class(self):
# Run once at startup, eg. create database structure
print("\nsetup testsuite environment")
open("/tmp/test2.out", "w").write("0")
@classmethod
def teardown_class(self):
# Run once at teardown, eg. drop database
print("\ncleanup testsuite environment")
os.unlink("/tmp/test2.out")
def setup(self):
# Before every test, like populate a table
print("\nbefore_every_test")
def teardown(self):
# After every test, eg truncate a table
print("\nafter_every_test")
def test_a(self):
assert os.path.isfile("/tmp/test2.out")
def test_b(self):
assert False
#
# Bonus examples:
#
# - testing exceptions
# - parametrized tests
#
@raises(IOError)
def test_except(self):
assert open('/tmp/MISSING')
def test_except_complex(self):
with assert_raises(IOError) as ex:
fh = open('/tmp/MISSING')
# Verify exception *and* errno!
assert_equals(ex.exception.errno, errno.ENOENT)
def test_parametrized(self):
# Runs 3 tests using the nosetest generator syntax.
for i in range(3):
# You have to yield a method to make it work into
# a class
yield self.harn_greater, i, 0
def harn_greater(self, a, b):
assert_greater(a, b)
# Try it!
!nosetests -v -s 02_nosetests_full.py # -s shows STDOUT