Running a Unit Test for multiple functions

6 次查看(过去 30 天)
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?

回答(4 个)

Andy Campbell
Andy Campbell 2015-8-4
编辑:Andy Campbell 2015-8-4
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 2015-8-4
编辑:Matt J 2015-8-4
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 个评论
Matt J
Matt J 2015-8-4
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 2015-8-4
It took some time to implement, but this is perfect, thank you.

请先登录,再进行评论。


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

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2015-8-4
编辑:Sean de Wolski 2015-8-4
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 个评论
Connor Gill
Connor Gill 2015-8-4
Unfortunately, there is no such network location. Cody Coursework, though, looks promising, thank you.
Andy Campbell
Andy Campbell 2015-8-4
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Test Execution 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by