Brief overview, examples, and resources to getting started with MATLAB's Unittest Framework.
What is a 'unit test?' A unit test is a way of testing a unit - the smallest piece of code (e.g., function or program) that can be isolated and tested for correctness, i.e., it works as intended.
"[unit testing] is a software testing method by which individual units of source code - sets of one or more computer modules together with associated control data, usage procedures, and operating procedures - are teste to determine whether they are fit for use." - Wikipedia
For more information, check out this wiki page.
Unit testing is available through MATLAB Testing Frameworks. There are several ways to write unit tests in MATLAB:
In general, the class-based unit tests are the most advanced and feature-rich way to write unit tests. In order of simple to advanced:
- script-based
- function-based
- class-based
The main advantages of function and class based unit tests are the use of fixtures and testCase qualifications.
For a detailed overview of the different types of MATLAB Unit Tests, see the Ways to Write Unit Tests.
Naming of unit tests should follow the MATLAB-defined conventions for each type of unit test. In general, the following convention can be used for every type of unit test:
- The file containing the unit tests must start or end with 'test'.
- The unit test names should start with 'test'.
For specific conventions for how to name unit tests, see additional resources.
- Write a unit test file (
fooTest.m
) - Write a function/program (
foo.m
) - Run the unit test(s)
results = runtests('foo')
results = run(foo)
Writing unit tests can be found here:
Examples of writing unit tests with scripts, functions, and classes are in Examples.
For more advanced topics like fixtures (test setup and teardown), parameterized tests, or creating test suites, see additional resources.
Additional information on unit test naming styles. Unit tests should convery easy-to-understand information about what is being tested.
Unit test qualifications are verifiable, assumable, or assertable 'checks' used to determine the outcome (i.e., pass/fail) of the unit test.
See more information on all the available unit test qualifications.
Test fixtures are used to perform setup and teardown actions before each test or set of unit tests. For example, adding and removing a folder to path to perform the test(s).
Perform unit test(s) with multiple values.
Please open an issue or feature reqeust if you would like to contribute. All suggestions and feedback are welcome.
If you'd like to add content directly, fork this repo, add your content and submit a pull request.
Please feel free to reach out and email me with the subject as the repo name.