Grading with Python UnitTest

Grading with Python UnitTest

The Advanced Code Tests in Python starter pack has a good rundown of getting a Python unit test running in Codio. The student code and test file need to be in the same directory. Since test scripts should live in .guides/secure, either use a helper script to copy the student code to the same directory as the test file, or append the path to include the location of the student file (as shown in the example below).

import unittest
from math import pi
import sys
sys.path.append("/home/codio/workspace/student_code/")
from circle import circle_area


class TestCircleArea(unittest.TestCase):
  def test_area(self):
    self.assertAlmostEqual(circle_area(1), pi)
    self.assertAlmostEqual(circle_area(0), 0)
    self.assertAlmostEqual(circle_area(2.1), pi * 2.1 ** 2)
    
  def test_values(self):
    self.assertRaises(ValueError, circle_area, -2)
    
  def test_types(self):
    self.assertRaises(TypeError, circle_area, 3+5j)
    self.assertRaises(TypeError, circle_area, True)
    self.assertRaises(TypeError, circle_area, "3")
    
if __name__ == '__main__':
    unittest.main()