Parse Student 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.

The following example looks for the for keyword and checks that the student has used a for loop at least 1 time.

Note: You may want to add more logic to this code if you heavily rely on it. I have it check if the for is on a line with a comment or in a puts statement but technically there are other ways for students to pass the code by putting a for somewhere.

Grading Test:

require 'rspec'
require 'stringio'

RSpec.describe "CodingRoomsUnitTests" do

    describe "Code requires at least 1 FOR loop" do
        it "checking that coding has >= 1 FOR loop" do
            count = 0
  "/usercode/example.rb").each do |line|
                if (line.include?('for') and !(line.include?('#') or line.include?('puts')))
                    count += 1
            expect(count).to be >= 1


Last updated