How can the code tell if a single test is running instead of the whole test class?
8 views (last 30 days)
Show older comments
I'd like to see more output when I run a single test, vs when I run a whole test class or suite.
Almost always, this is because I'm running it several or many times while debugging, launching the single test with the command-return shortcut. This is a different use case from running all the tests in a file, much less all the tests in a suite. You can think of it like raising the log level
Is there a way, either in a test, or in one of the TestClassSetup (setupOnce() or set()) methods that the code can determine this?
PS: The answer seems to be "no."
2 Comments
Steven Lord
on 29 May 2020
Why do you want to see more output when you run one test method instead of a test class or test suite?
Ideally your test methods should be completely independent of one another. They should neither know nor care whether or not another test method is running in the same test run.
Answers (2)
Sean de Wolski
on 29 May 2020
Having the tests needing to know about how they are running is backwards. The tests should always do the same thing - test the code and provide outputs. Plugins added to the TestRunner should decide whether to absorb these outputs or not.
So if you want additional outputs, add plugins to the TestRunner. It may be a custom plugin, it may just be withTextOutput. You could write a few convenience functions to run the test suite with or without test this level of output.
Steven Lord
on 29 May 2020
If you're running this to debug, I assume you actually want the additional information only when the test fails. If so try matlab.unittest.TestCase.onFailure.
If not, if you want explicit control over the logging level, how are you running the test?
- With runtests? Specify the LoggingLevel or OutputDetail in your runtests call.
- With run on a TestRunner object? Try adding a plugin to the TestRunner as Sean suggested.
- In the Editor or Live Editor? Click the downward facing triangle under Run Tests and change the output detail or logging level.
2 Comments
See Also
Categories
Find more on Run 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!