Parse Student Code

Check if student used _____ in their code.

Often you just want to make sure students successfully used a particular function/command/structure. The simplest way I have found to do this is just parse the student's code file and make sure what you are looking for is there.

Example 1 - Used ____ function ____ times

For example, maybe you want to make sure the student used the input function 4 times. We can parse the student's code file and count how many times "input(" is present in their code.

This test assumes the student's file name is example.py.

Grading Test:

import unittest

count = 0

class CodingRoomsUnitTests(unittest.TestCase):

    def test_default_case(self):
        global count
        f = open("example.py", "r")
        lines = f.readlines()
        f.close()
        
        for line in lines:
            line = line.strip().split()
            for words in line:
                if words.find("input(") != -1:
                    count += 1
                    
        self.assertTrue(count == 4)
        

if __name__ == '__main__':
    unittest.main()

Notice that the test asserts that the count is equal to 4. Of course you can change this condition to anything you want. For example, if you want to make sure they have at least 1 input() you could use count >= 1.

Sample Solution:

example.py
name = input("What is your name?")
age = input("What is your age?")
hair = input("What color is your hair?")
place = input("Where do you live?")

Example 2 - Used ____ thing ____ times

Sometimes if you are checking for a specific character/symbol it is better to use the .count() method so you do not miss an instance of the character/symbol.

This example checks that the student used the + addition at least 2 times.

Grading Test:

import unittest

count = 0

class CodingRoomsUnitTests(unittest.TestCase):

    def test_default_case(self):
        global count
        f = open("example.py", "r")
        lines = f.readlines()
        f.close()
        
        for line in lines:
            line = line.strip().split()
            for words in line:
                if words.find("+") != -1:
                    count += words.count("+")

        self.assertTrue(count >= 2)

if __name__ == '__main__':
    unittest.main()

Example 3 - Used ____ function with these exact arguments

Since spaces can be used between arguments, to check if a function is called with exact arguments, you want to check each line of the student's code and remove all spaces. Then find the function call you are looking for.

This example checks that there is at least 1 call of randint(501,1000) in the student's code.

Grading Test:

import unittest

count = 0

class CodingRoomsUnitTests(unittest.TestCase):

    def test_default_case(self):
        global count
        f = open("example.py", "r")
        lines = f.readlines()
        f.close()
        
        for line in lines:
            line = line.strip().replace(" ", "")
            if line.find("randint(501,1000)") != -1:
                count += 1
                
        self.assertTrue(count >= 1)

if __name__ == '__main__':
    unittest.main()

Last updated