Running a Unit Test for multiple functions

7 views (last 30 days)
I teach an undergraduate level programming class, and many of the assignments require students to submit functions to be ran and graded. Using a unit test to grade them all seemed to be an easy solution. However, I can't seem to find a way to run one single test for multiple functions (with different names). Is there an easy way to do this without specifying the name of each individual function?

Answers (4)

Andy Campbell
Andy Campbell on 4 Aug 2015
Edited: Andy Campbell on 4 Aug 2015
Hi Connor,
Definitely look into Cody Coursework, but for now it only supports script-based testing, which does not have as many test authoring features. However, it has many features related to grading student code so check it out.
Otherwise you should look into parameterized testing. If you require your students to place their code into a certain folder according to some naming convention you can use this like so:
classdef Problem1Test < matlab.unittest.TestCase
properties(ClassSetupParameter)
student = generateClassList;
end
properties
StudentFunction
end
methods(TestClassSetup)
function findStudentFunction(testCase, student)
% Look for foo_AndyCampbell, foo_ConnorGill, etc
% and store the function handle
testCase.StudentFcn = str2func(['foo_' student]);
end
end
methods(Test)
function testItWorks(testCase)
testCase.StudentFcn();
end
end
end
function students = generateClassList
students = {'AndyCampbell', 'ConnorGill'};
end
A couple nice benefits:
  • If you have a static class list this can be shared across many different problems or assignments
  • If you have a student which does not submit a solution (or doesn't follow the instructed naming convention) their test will fail.
  • You can specifically select a particular student across different tests if you'd like. If you have Problem1_AndyCampbell, Problem2_AndyCampbell, Problem3_AndyCampbell, you can run all of mine like so:
>> runtests(pwd, 'ParameterName', 'AndyCampbell')
  • These parameters show in the failure diagnostics. For example:
================================================================================
Error occurred while setting up or tearing down Problem1Test[student=AndyCampbell].
As a result, all Problem1Test[student=AndyCampbell] tests failed and did not run to completion.
--------------
Error Details:
--------------
No public property StudentFcn exists for class Problem1Test.
Error in Problem1Test/findStudentFunction (line 14)
testCase.StudentFcn = str2func(['foo_' student]);
================================================================================
Failure Summary:
Name Failed Incomplete Reason(s)
=================================================================================
Problem1Test[student=AndyCampbell]/testItWorks X X Errored.

Matt J
Matt J on 4 Aug 2015
Edited: Matt J on 4 Aug 2015
If you put all the function mfiles in one directory, you can auto-grab all the names using the DIR command and then loop over them:
S=dir;
for i=1:length(S)
[p,funcname,ext]=fileparts(S(i).name);
if ~strcmp(ext,'.m'), continue; end
CodeOutput=eval(funcname);
if iscorrect(CodeOutput)
Grade(i)=...
end
end
  2 Comments
Matt J
Matt J on 4 Aug 2015
It might be a good idea to have the students name their mfiles with their own names so you can automatically determine whose submission belongs to whom.
Connor Gill
Connor Gill on 4 Aug 2015
It took some time to implement, but this is perfect, thank you.

Sign in to comment.


Steven Lord
Steven Lord on 4 Aug 2015
You might find this post or this other post on Loren's blog informative.
  1 Comment
Connor Gill
Connor Gill on 4 Aug 2015
That all is helpful, but it only applies to running a test on a single function.

Sign in to comment.


Sean de Wolski
Sean de Wolski on 4 Aug 2015
Edited: Sean de Wolski on 4 Aug 2015
You might want to consider having the same function name for all functions to be tested. Then test the same function in different folders. You could use a TestParameter to contain all folders to be tested and then call the function that would exist in each folder.
Andy will hopefully chime in as well.
  4 Comments
Connor Gill
Connor Gill on 4 Aug 2015
Unfortunately, there is no such network location. Cody Coursework, though, looks promising, thank you.
Andy Campbell
Andy Campbell on 4 Aug 2015
This is a good idea to use parameterized testing, but no need to place them in separate folders. You can put them all in the same folder as long as they have different function names.
Another alternative is to tell each student to put their code into their own package and use the package names as the test parameter.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!