Return Function Tests
Each example assumes the students file name is example#.py replacing # with the actual example number. You can call the functions by importing the student file name of your choice.

Example 1 - Add Five

Students write an function named add_five() that takes an integer as a parameter and returns 5 plus the parameter value.

Grading Tests:

import unittest
import sys, io
​
class CodingRoomsUnitTests(unittest.TestCase):
​
def test_default_case(self):
import example1
self.assertEqual(10, example1.add_five(5))
self.assertEqual(5, example1.add_five(0))
self.assertEqual(11, example1.add_five(6))
if __name__ == '__main__':
unittest.main()

Sample Solution:

example1.py
def add_five(number):
number += 5
return number

Example 2 - Average

Students write a function that takes 5 values as parameters and returns the average value of the 5 values. The function must be named average() and it must have 5 parameters.
Example: Calling average(1, 5, 7, 4, 10) would return 5.4.
Note: Almost Equal assertion is used with a delta value as tolerance when comparing values.

Grading Tests:

import unittest
import sys, io
​
class CodingRoomsUnitTests(unittest.TestCase):
​
def test_default_case(self):
import example2
self.assertAlmostEqual(5.4, example2.average(1, 5, 7, 4, 10), delta=0.001)
self.assertAlmostEqual(9.6, example2.average(5, 9, 24, 6, 4), delta=0.001)
if __name__ == '__main__':
unittest.main()

Sample Solution:

example2.py
def average(a, b, c, d, e):
sum = a + b + c + d + e
return sum / 5

Example 3 - Repeats

Students write a function that takes a parameter. If the String parameter has a double letter (i.e. contains the same letter twice in a row) then it should return true. Otherwise, it should return false.
This function must be named has_repeat() and have a parameter. This function must return a boolean.

Grading Tests:

import unittest
import sys, io
​
class CodingRoomsUnitTests(unittest.TestCase):
​
def test_default_case(self):
import example3
self.assertTrue(example3.has_repeat("mississippi"))
self.assertFalse(example3.has_repeat("capsized"))
self.assertTrue(example3.has_repeat("mazzone"))
self.assertFalse(example3.has_repeat("this"))
if __name__ == '__main__':
unittest.main()

Sample Solution:

example3.py
def has_repeat(word):
count = 0
for letter in word:
if letter == word[count-1]:
return True
count += 1
return False
Export as PDF
Copy link
On this page
Example 1 - Add Five
Example 2 - Average
Example 3 - Repeats