I would like to display the Exception message thrown in scala test.
" iWillThrowCustomException Method Failure test.
" should "Fail, Reason: Reason for failing. " in {
evaluating {
iWillThrowCustomException();
} should produce [CustomException]
}
If CustomExeption will throw different types of messages for differnt inputs , say
(for -ve amount - Amount is less than zero, for chars in amount - Invalid amount),
how to display the message which is thrown in the block, becuase it will through
the CustomException and it will show Test Success, but for which senario it has thrown the error
Alternatively you can check out intercept:
val ex = intercept[CustomException] {
iWillThrowCustomException()
}
ex.getMessage should equal ("My custom message")
evaluating also returns an exception so you can inspect it or print the message. Here is example from the ScalaDoc:
val thrown = evaluating { s.charAt(-1) } should produce [IndexOutOfBoundsException]
thrown.getMessage should equal ("String index out of range: -1")
As far as I know, you can't include exception message in the test name.
What you can do, is to add additional information about test with info():
"iWillThrowCustomException Method Failure test." in {
val exception = evaluating { iWillThrowCustomException() } should produce [CustomException]
info("Reason: " + exception.getMessage)
}
This will be shown in the test results as nested message. You can find more info about this in ScalaDoc.
Related
I have a block of try-exception code and I want to test if the block works properly? I wonder if there is a way to test this block with a unit test using pylint? This is the snipent I have:
class data_type(Enum):
SESSIONS = 0
RUNS = 1
def find_filter(data_type)
try:
if data_type.name == 'SESSIONS':
return {}
elif data_type.name == 'RUNS':
#Filter out rerun jobs
filter = {
"filter":{
"attName":"is_rerun",
"operand":"IN",
"#c":".InFilter",
"values":["FALSE"]
}
}
return filter
except AttributeError as erro:
loghandler.critical('Error in creating count query: {}'.format(erro))
I want to test if the data_type is not a valid enum member, the exception catches it before the execution goes to if statement. For example if:
find_filter('')
is called I like to see that the except AttributeError as erro captures the error and the error is logged.
Is there a way to do that?
PS: I know with pytest.raises() I can test if a raised error is really raised? But as far as I understand pytest.raises() does not work for testing the exception block of a try-exception case.
Any suggestion please?
Thanks.
Coming from a Java background, I have been trying to teach myself Scala for some time now. As part of that, I am doing a small pet project that exposes a HTTP endpoint that saves the registration numberof a vehicle against the owner and returns the status.
To give more context, I am using Slick as FRM which performs DB operations asynchronously and returns a Future.
Based on the output of this Future, I want to set the status variable to return back to the client.
Here, is the code
def addVehicleOwner(vehicle: Vehicle): String = {
var status = ""
val addFuture = db.run((vehicles returning vehicles.map(_.id)) += vehicle)
addFuture onComplete {
case Success(id) => {
BotLogger.info(LOGTAG, s"Vehicle registered at $id ")
status = String.format("Registration number - '%s' mapped to owner '%s' successfully", vehicle.registration,
vehicle.owner)
println(s"status inside success $status") //--------- (1)
}
case Failure(e: SQLException) if e.getMessage.contains("SQLITE_CONSTRAINT") => {
status = updateVehicleOwner(vehicle)
BotLogger.info(LOGTAG, s"Updated owner='${vehicle.owner}' for '${vehicle.registration}'")
}
case Failure(e) => {
BotLogger.error(LOGTAG, e)
status = "Sorry, unable to add now!"
}
}
exec(addFuture)
println(s"Status=$status") //--------- (2)
status
}
// Helper method for running a query in this example file:
def exec[T](sqlFuture: Future[T]):T = Await.result(sqlFuture, 1 seconds)
This was fairly simple in Java. With Scala, I am facing the following problems:
The expected value gets printed at (1), but (2) always prints empty string and same is what method returns. Can someone explain why?
I even tried marking the var status as #volatile var status, it still evaluates to empty string.
I know, that the above is not the functional way of doing things as I am muting state. What is the clean way of writing code for such cases.
Almost all the examples I could find described how to map the result of Success or handle Failure by doing a println. I want to do more than that.
What are some good references of small projects that I can refer to? Specially, that follow TDD.
Instead of relying on status to complete inside the closure, you can recover over the Future[T] which handle the exception if they occur, and always returns the result you want. This is taking advantage of the nature of expressions in Scala:
val addFuture =
db.run((vehicles returning vehicles.map(_.id)) += vehicle)
.recover {
case e: SQLException if e.getMessage.contains("SQLITE_CONSTRAINT") => {
val status = updateVehicleOwner(vehicle)
BotLogger.info(
LOGTAG,
s"Updated owner='${vehicle.owner}' for '${vehicle.registration}'"
)
status
}
case e => {
BotLogger.error(LOGTAG, e)
val status = "Sorry, unable to add now!"
status
}
}
val result: String = exec(addFuture)
println(s"Status = $result")
result
Note that Await.result should not be used in any production environment as it synchronously blocks on the Future, which is exactly the opposite of what you actually want. If you're already using a Future to delegate work, you want it to complete asynchronously. I'm assuming your exec method was simply for testing purposes.
I am raising an exception and trying to handle the exception in snippet. The raising exception part and handling exception part are done in a function. Is it wrong to do so?
import sys
def water_level(lev):
if(lev<10):
raise Exception("Invalid Level!")
print"New level=" # If exception not raised then print new level
lev=lev+10
print lev
try:
if(level<10):
print"Alarming situation has occurred."
except Exception:
sys.stdout.write('\a')
sys.stdout.flush()
else:
os.system('say "Liquid level ok"')
print"Enter the level of tank"
level=input()
water_level(level) #function call
The output is not handling exception. Can someone explain me why?
It is better to just raise the exception in the function and then catch it it when you call the function so your function does not do too much and your error handling is independent. And it makes your code simpler.
Your code never reached your except clause because if the water level is too low it raises an exception and jumps out of the function and if it was okay it just reaches the else clause. The print statement in your try clause is also never reached because it is the same condition than the one that raises your exception and that one jumps out.
Your code should be something like that...
import sys
import os
def water_level(level):
#just raise exception in function here
if level < 10:
raise Exception("Invalid Level!")
level = level + 10
print("New level=") # If exception not raised then print new level
print(level)
#function call
print("Enter the level of tank")
#cast to int
level=int(input())
try:
#call function in here
water_level(level)
#catch risen exceptions
except Exception as e:
sys.stdout.write('\a')
sys.stdout.flush()
#print exception(verification)
print(e)
print("Alarming situation has occurred.")
else:
os.system('say "Liquid level ok"')
Note that i corrected some other flaws
import os was missing
you should cast your input() to a number so you can do number comparisions and additions
try to avoid to catch the least specific Exception Exception because you will catch every other Exception too.(thats why i added the print(e)) -> think about custom exceptions
How can display the original stacktrace for a Try Failure as part of the test output?
When I do the following:
result.success.value should equal blah
And the result is a Failure I get
The Try on which success was invoked was not a Success.
Or, if I do this first:
result should be a 'success
It's a little more informative, because I can see the exception:
Failure(java.lang.IllegalArgumentException: Cannot format given Object as a Date) was not a success
But the stack trace shows where it failed in the test, not the original stack trace of the Failure.
If some error-causing code is wrapped into Try, it means that exception is handled somewhere inside calculation, and it's cause and message will not be printed somewhere until explicitly requested. In order to see original cause, you can access failure object directly (docs), or handle failure manually:
val (ok, message) = result match {
case Success(v) => (true, "")
case Failure(ex) =>
(false, ex.getMessage + "\n" + ex.getStackTrace.mkString("\n"))
}
assert(ok, message)
This link also looks helpful.
If you want original stack trace to be printed - use result.get, but check not for 'success but inner type/value:
result.get should equal blah
In our Scala, Play, Reactivemongo we have a big problem with exception handling - when there is an error, in Iteratee/Enumeratee or in Actor system Play just swallows it, without logging any error to the output. So we effectively need to guess where, and why this error might happen.
We made Globals override, to always print the error, and specified logger.root=TRACE, but still saw no output, from which we could analyse our problems.
How to forcebly make Play print all the errors
Didn't found the way to explicitly log everything but there is a way to log exceptions locally.
I did this:
def recover[T] = Enumeratee.recover[T] {
case (e, input) => Logger.error("Error happened on:" + input, e)
}
and then used it on all the enumeratees that can produce errors
def transform(from: Enumerator[From]): Enumerator[String] = {
heading >>> (from &> recover[From] ><> mapper) >>> tailing
}
here, mapper throws exception, and they are all logged.
I think your problem is with how Future works in scala, let's take the following exemple :
val f :Future[Int]= Future {
throw new NullPointerException("NULL")
1
}
f.map(s1 => {println(s" ==> $s1");s" ==> $s1"})
This code will throw an exception but the stack trace will not be printed as futures handle the error.
If you want to get the error that happened you can
just call:
f.onComplete{
case Success(e) => {}
case Failure(e) => e.printStackTrace()
}
e is a throwable that you can use as you want to handle the error.
At the solution I used, is override through ErrorHandling in Play https://www.playframework.com/documentation/2.4.2/ScalaErrorHandling, basically creating ErrorHandler that logs all the errors, with needed detalization.