Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubs in Matlab Toolbox Testing

As a follow up to my previous question, I run into a new obstacle: how to generate stubs for functions in a toolbox?

I found Andy Campbell's solution for the non toolbox case. This does not work in my case because Matlab complains: package directories are not allowed in MATLAB path in Pathfixtures!

I also don't see how this concept will overwrite the import statements within the toolbox, e.g. in file2.

This is my setup:

+folder1/file1.m
+folder1/runtestsuite.m
+folder1/unittest_data/file1_testdata.mat
+folder1/+folder2/file2.m
+folder1/+folder2/unittest_data/overloads/file1.m
...

Let's say I want to stub file1 in file2. And file2 has as a first statement: import folder1.file1.

With

methods(Access=private)
        function inject_file1_stub(testCase, answer)
            import matlab.unittest.fixtures.PathFixture;
            testCase.applyFixture(PathFixture(fullfile(testCase.path,'overloads')));
            file1('', answer);
        end
    end

So currently I believe this concept is not applicable in my case, so how is this done correctly with matlab?

I know one can shadow an implementation of a function in a toolbox, if one adds another path with the same toolboxname and function to the path. For this I would have to recreate a subset of the current folder setup:

So my current idea for a fixture is

  1. create temporary folder with tempdir
  2. use mfilename to check what subset of the toolbox directories I have to recreate
  3. generate folder structure
  4. copy from the overload folder to the new toolbox system
  5. Add this to path

Run tests

In teardown

  1. remove the temporary folder
  2. remove the entry from path

I have not implemented this yet, and seems a bit redundant knowing that there is a Pathfixture in matlab already. Pointers to other toolboxes which show how they have solved these kind of problems are also welcome.

like image 470
Bort Avatar asked Mar 31 '26 01:03

Bort


1 Answers

It is true that you can't add subfolders of packages to the path, but that doesn't mean you can't shadow these path functions. To do this you need to separate the test related content out of your source location. For example, if your source looks like:

<source-home>/+folder1/file1.m
<source-home>/+folder1/+folder2/file2.m

Then you can put your tests somewhere else so your structure would look something like:

<test-home>/file1Test.m
<test-home>/file2Test.m % could also put tests into packages if you want
<test-home>/overloads/+folder1/file1.m
<test-home>/overloads/+folder1/+folder2/file2.m

Then inside of file1Test and/or file2Test you would use a PathFixture to add:

<test-home>/overloads/

to the path.

Also, another thing to consider is defining an interface in your source code for these dependencies and leveraging dependency injection (with or without a DI framework) in order to get test specific behavior into your tests.

like image 170
Andy Campbell Avatar answered Apr 02 '26 18:04

Andy Campbell