Skip to content
E. Lynette Rayle edited this page May 25, 2022 · 2 revisions

Clear the MATLAB toolbox file path cache

Reference: Toolbox Path Caching in MATLAB (MATLAB doc)

If MATLAB is picking up stale code from the toolbox area, you can fix this by clearing the toolbox cache using one of two methods:

Method 1: Thru UI

  • File -> Preferences -> General
  • Click Update Toolbox Path Cache button

Method 2: Thru command line

  • Type the following two commands in the command window
    • rehash toolbox
    • rehash toolboxcache

Auto-add paths at startup

  • Edit file: MATLAB\R2010a\toolbox\local\matlabrc.m
  • Goto bottom of file
  • Add...
addpath(genpath(strcat(matlabroot,'\toolbox\dm-matlab-messageQueue')));

Understanding Position

  • pos = distance from left, distance from bottom, width, height
  • set(handle, 'Position', pos)

Helpful Shortcuts

  • fcd FUNCTION_NAME - finds the function on the current path and changes to the directory where the file defining the function lives

MatLab xUnit Testing

Installation: 

  • download from: MatLab FileExchange for xUnit Testing Framework
  • unzip to:  C:\Program Files\MATLAB  
    • will create matlab_xunit in the MATLAB directory
    • your path may not be exactly the same depending on where MatLab is installed
  • read:  C:\Program Files\MATLAB\matlab_xunit\Readme.html   (very good examples)
  • add:  C:\Program Files\MATLAB\matlab_xunit\xunit  to the MatLab path

Various ways to run tests...

Test Directory:  Run all tests within a single directory

  1. In MatLab, navigate to the directory holding the tests, such that it is the current directory.
  2. Type runtests
cd c:\XBAT_R6_Tests\Example\testFliplrDir    (in MatLab)
runtests

% get more information from runtests
runtests -verbose

NOTE: With this method, the directory does not have to be on the MatLab path. NOTE:  File fooFliplrVector.m was not considered a test.  Only files with name test*.m are selected as a test.

Anatomy of a test...
  • Structurally the same as any function.
  • To be automatically picked up, the file name needs to begin with test.
  • Test a condition and use MatLab's error() function or one of xUnit's assert functions to report problems.
Ex. 1: Using error()        - Look at ...\Example\testFliplrDir\testFliplrVector.m
Ex. 2: Using assertEqual()  - Look at ...\Example\testFliplrDir\testFliplrMatrix.m
Assertion Methods
What a failing test looks like...

Ex. 1 Test Fails when using error()

   Edit ...\Example\testFliplrDir\testFliplrVector.m
   Remove ~ in front of isequal on line 8
   Save
   runtests
   Add ~ back and Save

Ex. 2 Test Fails when using assertEqual()

   Edit ...\Example\testFliplrDir\testFliplrMatrix.m
   Change [3 2 1] to [2 2 1] on line 5
   Save
   runtests
   Change back and Save
Single Test:  Run a specific test
  1. Navigate to the directory holding the tests, such that it is the current directory.
  2. Type runtests testname
cd ...\Example\testFliplrDir
runtests testFliplrMatrix

% location of verbose on the parameter string doesn't matter
runtests -verbose testFliplrMatrix
runtests fooFliplrVector -verbose
Test Suite: Define multiple tests in a single file and run
  1. Navigate to the directory holding the test suite, such that it is the current directory.
  2. Type runtests testname
cd ...\Example

% run all tests in the suite
runtests testFliplrSuite -verbose

% run one test in the suite
runtests testFliplrSuite:testFliplrMatrix -verbose
Anatomy of a test suite...
  • Primary function needs to call initTestSuite and return test_suite
  • All test functions follow the same pattern as for a single test in a single file.
    • Function name begins with test.
    • Test a condition and use MatLab's error() function or one of xUnit's assert functions to report problems.
Twists

Ex. 1 Test name doesn't begin with test.

   Edit ...\Example\testFliplrSuite.m
   Line 4: Change test name from testFliplrMatrix to fooFliplrMatrix
   Save
   runtests testFliplrSuite -verbose
   Change back and Save

Ex. 2 Multiple assert statements.

   Edit ...\Example\testFliplrSuite.m
   Line 6: Copy assertEqual entire line and paste below to line 7.
   Line 7: In the copied line, change [3 2 1] to [2 2 1].
   Save
   runtests testFliplrSuite -verbose

   Switch lines 6 & 7 so that the test fails on the first assert.
   Save
   runtests testFliplrSuite -verbose

   Fix both lines 6 & 7 to use [3 2 1] instead of [2 2 1] so that both assert statements will pass.
   Save
   runtests testFliplrSuite -verbose

   Remove editing by removing line 7, the second assertEqual, and Save

Ex. 3 Compared to tests in a directory.

   cd ...\Example
   runtests testFliplrDir -verbose

   runtests testFliplrDir:testFliplrMatrix -verbose

   cd ...\Example\testFliplrDir
   runtests testFliplrMatrix -verbose

Test Package: Define multiple tests in a package and run

  1. Navigate to the directory holding the package, such that it is the current directory.
  2. Type runtests packagename
cd ...\Example

% run tests in package
runtests testFliplrPkg -verbose

% run tests in package & sub-package
mkdir +testSubPkg
mv testFliplrSuite.m +testSubPkg
runtests testFliplrPkg -verbose

Anatomy of a test package...

  • Directory name starts with + (ex. +testFliplrPkg), same as all packages in MatLab.
  • Package directory can contain...
    • Individual test functions
    • Test suites
    • Test classes
    • Sub-packages with more tests
  • runtests will run all tests, test suites, and test classes in the package and its sub-packages.
Twists

Ex. 1 Test package and sub-packages do not have to begin with test.

   cd ...\Example
   mv +testFliplrPkg +fooFliplrPkg
   runtests fooFliplrPkg -verbose

   mv +fooFliplr +fooFliplr
   runtests fooFliplr -verbose

   cd +fooFliplr
   mv +testSubPkg +fooSubPkg
   cd ..  (back to Example)
   runtests fooFliplr -verbose

Ex. 2 Compared to tests in a non-package directory.

   cd ...\Example\testFliplrDir
   mkdir testSubDir
   cp ..\testFliplrSuite.m testFliplrDir
   cd ..    (back to Example)
   runtests testFliplrDir -verbose
Clone this wiki locally