Running a Unit Test for multiple functions

4 ビュー (過去 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 Center および File ExchangeTesting Frameworks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by