フィルターのクリア

Prevent Test Browser from changing working directory

6 ビュー (過去 30 日間)
Andrés Ferreiro
Andrés Ferreiro 2023 年 11 月 30 日
編集済み: Andrés Ferreiro 2023 年 12 月 14 日
I've just upgraded from 2021a to 2023b. All my project tests use test data loaded from binary files, specified as relative paths within the project (data/inputs/*.bin). The new Test Browser breaks my test execution inside the GUI because it changes the working directory to the location of each individual test, making the relative paths to the test files non working. I know I can still use the "runtests" function, but I really loved the possibility of running a single test inside a file with just a click. Is there a way to tune the Test Browser (or opt out from its use)?
Thanks in advance.

回答 (1 件)

Kartik Saxena
Kartik Saxena 2023 年 12 月 4 日
Hi,
You can add a setup function to your tests that changes the working directory to the correct location. This setup function would run before each test and ensure the working directory is set properly.
Here's an example of how you might write such a setup function:
function tests = yourTestFunction
tests = functiontests(localfunctions);
tests(1).Setup = @setupFunction;
end
function setupFunction(testCase)
% Get the current file's directory
currentTestFile = mfilename('fullpath');
[currentDir, ~, ~] = fileparts(currentTestFile);
% Change to the directory where your project's root is
% For example, if your test file is in 'project/tests' and your data is in 'project/data'
% you would navigate up one level to 'project'
cd(fullfile(currentDir, '..'));
end
I hope this resolves your issue.
  1 件のコメント
Andrés Ferreiro
Andrés Ferreiro 2023 年 12 月 14 日
編集済み: Andrés Ferreiro 2023 年 12 月 14 日
That's an approach, but having more than 100 test files, I'd prefer to avoid modifying each one. Moreover, the data locations are nested inside data and also the test files insed tests, so this is an approach quite cumbersome for my use case as I'm looking for some Matlab level setting or something. Thanks anyway! The setup (and teardown) functions are quite useful in many cases.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResults, Reporting, and Test File Management についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by