I would like to write a unit test for a method on an ENUM class, using all possible enumerations of that ENUM class as a TestParameter. The ENUM class, as well as its corresponding unit test, is part of a repository, so I'd like to be diligent when setting up the paths to ensure that neither the unit test nor the ENUM itself is dependent on any code I have locally on my machine (and similar for any developers). Therefore I'd like to utilize PathFixtures to handle adding and removing from the Matlab path. I can't figure out how to set the TestParameter properties given this setup, since the TestParameter block requires the ENUM class to be on the path.
The ENUM classdef m-file (MyClass.m) looks like this:
classdef MyClass enumeration A( 1, 2 ) B( 3, 4 ) end properties prop1 prop2 end methods function this = MyClass( input1, input2 ) this.prop1 = input1; this.prop2 = input2; end function output = SomeMethod( this ) output = this.prop1 + this.prop2; end endend
The unit test code (MyClassTest.m) looks like this:
classdef MyClassTest < matlab.unittest.TestCase properties( TestParameter ) allPossibleEnums = arrayfun( @( x ) x, ... eval( 'enumeration( ''MyClass'' )' ), ... 'UniformOutput', false ); end methods( TestClassSetup = true ) function SetUpPaths( testCase ) testCase.applyFixture( matlab.unittest.fixtures.PathFixture( ... PathToMyClass ) ); end end methods( Test = true, ParameterCombination = 'sequential' ) function SomeMethodTest( testCase, allPossibleEnums ) output = allPossibleEnums.SomeMethod(); check = allPossibleEnums.prop1 + allPossibleEnums.prop2; testCase.verifyEqual( output, check ); end endend
My issue is that the properties block to define allPossibleEnums requires PathToMyClass be on the Matlab path prior to running the constructor for MyClassTest. I tried using a SharedTestFixtures for MyClassTest but that appears to require a hard-coded path, and I'd like other developers to be able to run the unit test without having to modify it when they check it out. I could also write a script and add the path to MyClass with an addpath( … ) call prior to constructing the MyClassTest, but that loses the robustness I'd like when using a PathFixtures. I could also hard-code allPossibleEnums as a cell array of strings containing all the possible enumerations, but that would need to be updated by hand should the enumeration list change. And I could simply leave the TestParameter properties empty and just create the enumeration list within the SomeMethodTest method and write a "for" loop, but that would seem to defeat the purpose of having a "sequential" parameter option and wouldn't allow me to see which enumeration caused a failure (I would also have to repeat the creation of the enumeration list in any additional test methods).
Is there any way around this? Am I simply misunderstanding how to use the unit test framework or the PathFixtures? Is there a different/better way to list out all the possible enumerations of MyClass as a TestParameter?
Thanks!
Best Answer