Nose Testing Framework
Contents
1. Reference
2. Usage
2.1. Simple Usage
#!/usr/bin/env python
#
# For a file f with content:
# 1, 2, 3, 4, 5
# 4, 5, 6, 7, 8
#
# csvread(f) should return:
# [ [1, 2, 3, 4, 5], [4, 5, 6, 7, 8]]
import csv
def csvread(f):
reader = csv.reader(f, delimiter=",", quotechar='"')
def clean(data):
return [ x.strip() for x in data]
return [clean(row) for row in reader]
from StringIO import StringIO
class TestCSVRead:
def setUp(self):
self.strobj = StringIO("1, 2, 3, 4, 5\n4, 5, 6, 7, 8\n")
def test_correct_lines(self):
"Returns correct number of lists for the given number of lines"
assert len(csvread(self.strobj)) == 2
def test_correct_cols(self):
"Returns correct number of columns of data"
assert len(csvread(self.strobj)[0]) == 5
def test_correct_data(self):
"Returns correct data"
out = csvread(self.strobj)
assert out == [ ["1", "2", "3", "4", "5"],
["4", "5", "6", "7", "8"]
]
# normal usage. Pretty silent $ nosetests csvread.py ... ---------------------------------------------------------------------- Ran 3 tests in 0.001s OK # verbose usage. more detail. $ nosetests -v csvread.py Returns correct number of columns of data ... ok Returns correct data ... ok Returns correct number of lists for the given number of lines ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.002s OK
