Is the class-, function-, or script-based testing best for Unit Testing?
4 views (last 30 days)
Show older comments
MathWorks Support Team
on 2 Jul 2021
Answered: MathWorks Support Team
on 2 Jul 2021
I want to unit-test my code but MATLAB has multiple approaches based on classes, scripts and functions. Which is the best?
Accepted Answer
MathWorks Support Team
on 2 Jul 2021
Class-, function-, and script-based testing are all equally valid approaches. All three are also valid xUnit implementations. Which one is used is determined by the features one wants to use and what style he prefers and is comfortable with writing.
Class-based tests provide the most features (e.g., code sharing via inheritance, shared test fixtures, parameterized testing) while script-based tests offer fewer features. On the other hand, script-based tests are much easier to write, especially if one is not comfortable with object-oriented programming. Function-based tests are somewhere between class- and script-based tests on the ease-of-use vs. capability spectrum.
If you are interested in taking advantage of the most powerful features in the testing framework (and not averse to writing classes), it would be suggested to write class-based tests.
0 Comments
More Answers (0)
See Also
Categories
Find more on Write Unit Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!