0

I am utterly confused by the unittest documentation: TestResult, TestLoader, testing framework, etc.

I just want to tweak the way the final results of a test run are printed out.

I have a specific thing I need to do: I am in fact using Jython, so when a bit of code raises an ExecutionException I need to dig down into the cause of this exception (ExecutionException.getCause()) to find the "real" exception which occurred, where it occurred, etc. At the moment I am just getting the location of the Future.get() which raises such an exception, and the message from the original exception (with no location). Useful, but could be improved.

Shouldn't it (in principle) be really simple to find out the object responsible for outputting the results of the testing and override some method like "print_result"...

There is another question here: Overriding Python Unit Test module for custom output? [code updated] ... this has no answers, and although the questioner said 9 months ago that he had "solved" it, he hasn't provided an answer. In any event it looks horribly complicated for what is a not unreasonable way of wishing to tweak things mildly... isn't there a simple way to do this?

later, answer to MartinBroadhurst's question about documenting during the run:

In fact I could laboriously surround all sorts of bits of code with try...except followed by a documentation function ... but if I don't do that any unexpected exceptions obviously get ejected, ultimately being caught by the testing framework.

In fact I have a decorator which I've made, @vigil( is_EDT ) (boolean param), which I use to decorate most methods and functions, the primary function of which is to check that the decorated method is being called in the "right kind of thread" (i.e. either the EDT or a non-EDT thread). This could be extended to trap any kinds of exceptions ... which is something I did previously as a solution to this problem of mine. This then printed out the exception details there and then, which was fine: the stuff was obviously not printed out at the same time as the results of the unittest run, but it was useful.

But in fact I shouldn't need to resort to my vigil function in this "make-and-mend" way! It really should be possible to tweak the unittest classes to override the way an exception is handled! Ultimately, unless some unittest guru can answer this question of mine, I'm going to have to examine the unittest source code and find out a way that way.

In a previous question of mine I asked about what appear to be a couple of non-functioning methods of unittest.TestResult... and it does regretfully appear this is not implemented as the Python documentation claims. Similarly, a little bit of additional experimentation just now seems to suggest more misdocumentation : on the python documentation page for unittest they appear to have incorrectly documented TestResult.startTest(), stopTest(), etc.: the parameter "test" should not be there (the convention in this documentation appears to be to omit the self param, and each of these methods takes only the self param).

In short, the whole unittest module is surprisingly unwieldy and dodgy... I'm surprised not least because I would have thought others in more influential positions than me would have got things changed...

Community
  • 1
  • 1
mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • I have to disagree: the unittest module, as implemented in Python, gives you a stack trace when you have a fail, when according to you all you need is something saying which fail occurred. But in fact it also gives you a stack trace when an Exception occurs. So your characterisation of unit testing is too narrow. – mike rodent Feb 13 '16 at 18:05
  • Are the exceptions expected and predictable? –  Feb 13 '16 at 18:55
  • @MartinBroadhurst No. I'm aware of the unittest `assertRaises` stuff, if that's what you were wondering. I just want to adapt the documentation of (unexpected) Exceptions, as currently implemented in the unittest module, so it can cope with a Java (Jython) multi-threaded context. – mike rodent Feb 13 '16 at 19:02
  • Can you not just `print` the stacktrace or whatever if an exception is raised? This will be added to the test output when you run the tests. –  Feb 13 '16 at 19:10
  • Thanks for giving the matter thought! With your indulgence I'm going to add my reply to this to the bottom of the question rather than extend the comments... – mike rodent Feb 13 '16 at 19:14

0 Answers0