Understanding how testCase.addTeardown works with @path
    9 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Diaa
      
 el 27 de Jul. de 2022
  
    
    
    
    
    Comentada: Steven Lord
    
      
 el 27 de Jul. de 2022
            classdef GoodFixturesTest < matlab.unittest.TestCase
    methods(TestMethodSetup)
        function addPath1(testCase)
            p = addpath(fullfile(pwd, 'path1'));
            testCase.addTeardown(@path, p);
        end
        function addPath2(testCase)
            p = addpath(fullfile(pwd, 'path2'));
            testCase.addTeardown(@path, p);
        end
    end
    methods(Test)
        function runTest(~)
        end
    end
end
So, I need to deeply understand how addTeardown works in this line 
testCase.addTeardown(@path, p);
to remove the search paths of path1 and path2 and restore the original search paths in view of 
testCase.addTeardown(tearDownFcn,arg1)
In other words, what is the meaning of passing all the search paths (including path1 and path2) stored in p to the function handle @path in order to restore the original search paths without the full paths of the newly added subfolders path1 and path2?
0 comentarios
Respuesta aceptada
  Steven Lord
    
      
 el 27 de Jul. de 2022
        From the addpath documentation: "oldpath = addpath(___) additionally returns the path prior to adding the specified folders." So p does not include the path1 or path2 subdirectories (unless they were already on the path.)
This is a common pattern in MATLAB. Many functions that change some of the state of MATLAB return the state prior to the change to facilitate "undoing" the state change.
Of course, this example is a bit artificial to me. For this I wouldn't use addTeardown manually to restore the previous state. I'd create and apply a matlab.unittest.fixtures.PathFixture. The PathFixture will automatically restore the previous state of the path when it gets torn down.
2 comentarios
  Steven Lord
    
      
 el 27 de Jul. de 2022
				Looking at the matlab.unittest.fixtures.PathFixture class documentation page, see the "Add Folders to the Path for Testing" example for a short demonstration of how to use the fixture. Note that there's no call to addTeardown in that code. Essentially the teardown was added by applyFixture when it applied the PathFixture to the test session.
Run that test several times under different configurations and examine the state of the path (using the path function) before and after each run.
- Neither folderA nor folderB are on the MATLAB path before the test executes. In this case they should be on the path during the execution of the test method after the fixture has been applied, but neither will be on the MATLAB path after the test executes.
- Only folderA (not folderB) is on the MATLAB path before the test executes. As above they will both be on the path during the test execution after the fixture has been applied, but only folderA will be on the MATLAB path after the test finishes its execution.
- Both folderA and folderB are on the MATLAB path before the test executes. They will remain on the MATLAB path during test execution and will remain on the MATLAB path after the test finishes executing.
Más respuestas (0)
Ver también
Categorías
				Más información sobre Write Unit Tests en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

