Running a Unit Test for multiple functions

조회 수: 6 (최근 30일)
Connor Gill
Connor Gill 2015년 8월 4일
댓글: Andy Campbell 2015년 8월 4일
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?
  댓글 수: 1
Cedric
Cedric 2015년 8월 4일
Where will be your students M-Files stored?

댓글을 달려면 로그인하십시오.

답변 (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