필터 지우기
필터 지우기

Unit test can't see the function it needs to test

조회 수: 10 (최근 30일)
Louis
Louis 2020년 11월 12일
댓글: Louis 2020년 11월 13일
This is my first time creating unittest, and I am having trouble directing unitTestClass to see the function it tests, which lives in a different directory.
I have the following directory structure:
+utilityFUnctions/
utilityFunction1.m
utilityFunction2.m
+computeFunctions/
computeFunction1.m
computeFunction2.m
unit_tests
unitTestClass.m
test_data/
unitTestInputData.mat
unitTestExpectedOut.mat
mainFunction.m
runAllUnitTests.m
Here is the content of the runAllUnitTest.m:
This basically will run all tests in unit_tests folder
import matlab.unittest.TestSuite
suiteFolder = TestSuite.fromFolder('unit_tests');
result = run(suiteFolder);
And this is the content of unit_tests/unitTestClass.m:
classdef computeFunction1Test < matlab.unittest.TestCase
methods (Test)
function testCase1(testCase)
[input1, input2] = load('test_data/unitTestInputData.mat')
act = compute.computeFunction1(input1, input2);
exp = load('test_data/unitTestExpectedOut.mat')
testCase.verifyEqual(act, exp)
end
end
end
When I run runAllUnitTest.m I get the following error:
Undefined function 'compute.computeFunction1'
I suspect that at the time of execution, working directory is /unit_tests and therefore not recognizing compute directory and functions in the directory.
Is there a way to keep the current directory structure and make the unit test work?
  댓글 수: 3
Louis
Louis 2020년 11월 13일
+computeFunctions and +utilityFunctions are automatically in the Matlab's search path since they have + appended to them. I can run them at the top level (where the directories are visible), but I can't seem to run them from unit_test directory.
Louis
Louis 2020년 11월 13일
Thank you, addpath('parent_directory') did the trick!

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

채택된 답변

Steven Lord
Steven Lord 2020년 11월 13일
+computeFunctions and +utilityFunctions are automatically in the Matlab's search path
Not true. In fact, package directories cannot be added to the search path. If the parent directory of the package directory is not on the search path and it is not the current directory, those package directories will not be visible to MATLAB.
If you want those package directories to be accessible to MATLAB always, add their parent directory to the path. If you only want them to be available while the test is running, consider using a matlab.unittest.fixtures.PathFixture to temporarily add that package directory to the path in one of the test setup methods. If you do when the test is torn down the path will be restored to its previous state.
  댓글 수: 1
Louis
Louis 2020년 11월 13일
Thank you! addpath('parent_directory') did the trick!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by