From: Matt Davis Date: Sun, 21 Oct 2012 21:33:14 +0000 (-0700) Subject: Adding interim animals and test_animals files for everyone. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=468330521052e0ec2613b94f7c3c45d4eec726e2;p=swc-testing-nose.git Adding interim animals and test_animals files for everyone. --- diff --git a/4-SoftwareEngineering/animals.py b/4-SoftwareEngineering/animals.py index 411ba9a..edebb44 100644 --- a/4-SoftwareEngineering/animals.py +++ b/4-SoftwareEngineering/animals.py @@ -1,6 +1,8 @@ def read_animals(filename): """ - This function will read animal data from a file. + This function will read animal data from a file. It returns 4 lists, + one for each column in the file. + """ date = [] @@ -20,3 +22,15 @@ def read_animals(filename): f.close() return (date, time, animal, number) + + +def mean(l): + """ + Return the mean of a list of numbers. Returned value + should always be a float. + + """ + if len(l) == 0: + return None + + return float(sum(l)) / len(l) diff --git a/4-SoftwareEngineering/test_animals.py b/4-SoftwareEngineering/test_animals.py index 0cc9a0a..0f4a539 100644 --- a/4-SoftwareEngineering/test_animals.py +++ b/4-SoftwareEngineering/test_animals.py @@ -1,16 +1,38 @@ import animals + def test_read_animals_date(): - reference_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', + reference_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23'] date, time, animal, number = animals.read_animals('animals.txt') assert date == reference_date, 'Date is wrong' + def test_read_animals_time(): - reference_time = ['22:06', '14:12', '10:24', '20:08', '18:46'] + reference_time = ['21:06', '14:12', '10:24', '20:08', '18:46'] date, time, animal, number = animals.read_animals('animals.txt') assert time == reference_time, 'Time is wrong' + + +def test_mean1(): + l = [1] + assert animals.mean(l) == 1 + + +def test_mean2(): + l = [1, 2, 3] + assert animals.mean(l) == 2 + + +def test_mean3(): + l = [4, 2, 3, 5] + assert animals.mean(l) == 3.5 + + +def test_mean4(): + l = [] + assert animals.mean(l) == None