How can the code tell if a single test is running instead of the whole test class?

9 vues (au cours des 30 derniers jours)
Randy Strauss
Randy Strauss le 29 Mai 2020
Modifié(e) : Randy Strauss le 8 Juil 2020
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 commentaires
Steven Lord
Steven Lord le 29 Mai 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.
Randy Strauss
Randy Strauss le 29 Mai 2020
I edited the question: I want more output when I'm running a single test multiple times while debugging.

Connectez-vous pour commenter.

Réponses (2)

Sean de Wolski
Sean de Wolski le 29 Mai 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.
  1 commentaire
Randy Strauss
Randy Strauss le 29 Mai 2020
I edited the question: I want more output when I'm running a single test multiple times while debugging. It's not "backwards", it's a different use case.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 29 Mai 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 commentaires
Randy Strauss
Randy Strauss le 1 Juin 2020
Modifié(e) : Randy Strauss le 1 Juin 2020
Yes, the live editor. Yes, I can click the output level and uncheck it later.
And no, "onFailure" doesn't help. The output is often from an internal calculation long before failure status can be determined. (But that's good to know- thanks.)
I can also edit the test and then later delete the edit.
I'm ONLY wondering if the test can determine that it's being run singly, so I can automate this once, for all tests in the file, rather than have to change something before and after. The answer seems to be "no"...
Another good answer would be to write the code modularly, and test each part, so I don't have large or complex debugging tasks. Actually, I'm doing that. My task is to refactor a few thousand lines of code to make them testable, and write tests. I'm getting close...
Randy Strauss
Randy Strauss le 8 Juil 2020
Modifié(e) : Randy Strauss le 8 Juil 2020
Setting the logging level using the Live Editor helps, but now I have to pass the testcase into every object...
I suppose the best thing to do is write my own logger for my objects that uses the global error() and warning() if it has no test case, and initialize it with a test case if I'm running a test. This way I can use my own obj.error() and obj.warn(), obj.info() and obj.trace() instead of the cryptic log(1-4, msg)... Weird...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Run Unit Tests dans Help Center et File Exchange

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by